To write the programs on function on any language we should understand the following 8 components
1.Function declaration(prototype)
- Function declaration tells the compiler the return type of the functions, number of arguments, function return type of the arguments and order of arguments
- A function declaration can be inside or outside the main() but the before function call;
Syntax:
return_type func_name(arg1,arg2..argn);
Example:
The above function takes 2 inputs of type int and returns the output of type int
2.Function definition
- The set of statements which defines the task of the function
- The return type must be mentioned in function definition which is the same given in function prototype
Example :
int sum(int x,int y)// function defination
{
return x+y;
}
3.Function call
- The function call is a request to execute the function defined , till then function will not execute
- No return-type should be specified at the function call
- A user can call function any number of time and anywhere
Example :
int add(2,3);//invalid
add(2,3);//valid
4.Formal Parameters
- The parameters present in the function definition are formal parameters
- Formal parameters will receive value from the actual parameter
- Formal parameters must be variable type only
Example :
int sum(int x,int y)//func_defination
{
return x+y;
// x and y are formal parameters
}
5.Actual parameters
- Parameters passed during function call are called formal parameters
- Actual parameters make a communication to formal parameters
- They can be the variable, constant or expression type
Example :
add(2,3);//constant type actual parametres
add(x,y);//variable type actual parametres
add(2*3,7-2);//expression type actual parametres
6.Return statement
- Return type specifies the function output and allocates some space to function
- Return type also depends on arguments type
Example :
int sum(int,int);
float sum(int,float);
long fact(int);
- Generally, it is used at the function definition end, because it makes to exit the function module
- Another major purpose of the return statement, it exits from the function module and gives control back to the caller
#include"iostream"
using namespace std;
int demo()//fun definition
{
cout << "control acquired from main\n";
return 1;
//return0; error:multiple return types not allowed
}
main()
{
cout << "before call control under main\n";
cout << "controltranmsferreed from main to demo\n" << demo();
cout << "control return back from demo to main()\n" ;
}
output
before call control under main
controltranmsferreed from main to demo
control acquired from main
control return back from demo to main()
- here function call at line demo() transfers function call to from main() to demo(), “return 1� transfers control back to main()
- Only one return statement is allowed in a function definition
- void return type function cannot use the return statement
Example :
void add()
{
return a+b;//error:void cannot return
}
7.Function call
It is the function in which a function call is made
Example :
main()//fun call
{
add(2,3);
}
here main() is calle for add() function, main is requesting add() to execute
8.Function signature
It is function name excluding return type in the function definition
Example :
int add(int x,int y)
{
return a+b;
//add(int x,int y) is function signature
}