Tuesday, 27 March 2018

Function Calls

A function cab be called by simply using function name followed by a list of actual parameters (or arguments) if any, enclosed in parentheses.
For example:
main( )
{
float m;
m = mul(5.2, 3.17); /* function call */
cout<<m
}
When compiler encounters the function call, it transfer program control to the function mul( ) by passing values 5.2 and 3.17 (actual parameters) to the formal parameters on the function mul( ). The mul( ) will performs operations on these values and then returns the result. It will be stored in variable m. We are printing the value of variable m on the screen. There are a number of different ways in which the function can be called as given below:

mul(5.2, 3.71);
mul(m, 3.17);
mul(5.2, m);
mul(m, n);
mul(m+5.1, 3.17);
mul(m+3.6, m+9.1);
mul(mul(6.1,8.2), 4.5); 
Only we have to remember that the function call must satisfy type and number of arguments passed as parameters to the called function.


Function Prototype / Function Declaration
 Like variables, all functions in C++ program must be declared, before they are used in calling function. This declaration of function is known as prototype. It having following syntax of use:

function_type function_name (parameter list);  

 this is very similar to function header except the terminating semicolon.
For example, the function mul( ) will be declared as,
float mul(float, float);
Generally, prototype declarations as not necessary, if the function have been declared before it is used. A prototype declarations my be placed in two places in the program.
  • Above all the functions
  • Inside function definition
  We place declaration above all the functions this prototype is known as global prototype. That is, we can use this function in any part on the program. When we place the function definition in the local declaration section of another it is referred as local prototype.
 

1 comment:

Popular Posts

Categories

Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (85) R (69) Selenium Webdriver (2) Software (13) SQL (27)