Functions
Functions
O Level / A Level
Chapter - 6 : Functions
Function Arguments
If a function is to use arguments, it must declare variables that accept the values of the
arguments. These variables are called the formal parameters of the function. Formal parameters
behave like other local variables inside the function and are created upon entry into the function and
destroyed upon exit.
Parameter
A parameter is a special kind of variable, used in a function to refer to one of the pieces of
data provided as input to the function to utilse.
These pieces of data are called arguments.
Parameters are Simply Variables.
Formal Parameter
Parameter Written in Function Definition is Called “Formal Parameter.
Formal parameters are always variables, while actual parameters do not have to be
variables.
Actual Parameter
Parameter Written in Function Call is Called “Actual Parameter”.
One can use numbers, expressions, or even function calls as actual parameters.
Example
void display(int para1)
{
printf( “ Number %d “ , para1);
}
void main()
{ int num1;
display(num1);
}
void main()
{ int r;
r=max( 10,15);
print(“Result = %d “ , r);
}
Calling Functions
There are two ways to call a function.
1) Any function can be called by simply using its name and argument list alone in a statement, as i
n the following example.
i) If the function has a return value, it is discarded.
wait(12);
2) The second method can be used only with functions that have a return value.
i) Because these functions evaluate to a value (that is, their return value), they are
valid C expressions and can be used anywhere a C expression can be used.
ii) An expression with a return value used as the right side of an assignment statement.