Articles added in Mathematics Lab | Cyber Lab is now open | Astronomy Lab is now open | VLSI Lab is now open | 120 SEO Tips article published | More exciting articles coming soon!




3.1 Introduction to Functions

A function is a self-contained part of a program which is meant for doing some specific computations such as finding the factorial of a number, area of triangle, etc.
Functions can be classified into two categories, as explained below:

1) Library functions:

Functions whose declarations are available in some header files, are known as library functions.
e.g. sqrt(), isaplha(), setw(), pow(), etc.

2) User defined functions:

Functions that are directly defined in a program are known as user defined functions. A C++ program can be treated as a collection of functions, one of the functions should be the main function. Execution of a program will always start with the main function and other functions will be executed only if they are called or invoked.

Remember:

--> Function can be defined before or after main function. If a function is defined before main, than function prototype is not required.
--> Formal parameters – Variables that are declared in a function header are known as formal parameters. In the above example, “m” is a formal parameter of the function.
--> Actual parameters – Variables, constants of an expression, which are used in a function call are known as actual parameters. When a function is called, the actual parameters will be passed on to the corresponding formal parameters. In above example, fact(n) is function call and “n” is the actual parameter.

Rules:

--> Number of actual and formal parameters should be same.
--> Types of corresponding formal and actual parameters also should be same.
--> return statement – To send a value to calling point, return statement can be used in function. After executing return statement, the control will be transferred back to the calling point
Syntax: return (expression); or return expression;
-->If the function doesn’t return anything, then its data type should be “void” (e.g. void main()).

# void:

“void” is a keyword and a data type which will not be used for declaring normal variables. When a function is not returning anything (for e.g.: a function that is meant for displaying something), then the return type of the function should be void. Such a function is called as void function.







CACKLE comment system





Programming Resources
Computer Networking Fundamentals Android Application