Actual Arguments:
1. Arguments which are mentioned in the function in the function call are known as calling
function.
2. These are the values which are actual arguments called to the function.
It can be written as constant , function expression on any function call which return a value .
ex: funct (6,9) , funct ( a,b )
Formal Arguments:
1. Arguments which are mentioned in function definition are called dummy or formal
argument.
2. These arguments are used to just hold the value that is sent by calling function.
3. Formal arguments are like other local variables of the function which are created when
function call starts and destroyed when end function.
Basic difference between formal and local argument are:
a) Formal arguments are declared within the ( ) where as local variables are declared
at beginning.
b) Formal arguments are automatically initialized when a value of actual argument is
passed.
c) Where other local variables are assigned variable through the statement inside the
function body.
Note: Order, number and type of actual argument in the function call should be matched with the
order , number and type of formal arguments in the function definition .
PARAMETER PASSING TECHNIQUES:
1. call by value
2. call by reference
Call by value:
Here value of actual arguments is passed to the formal arguments and operation is done in the
formal argument.
Since formal arguments are photo copy of actual argument, any change of the formal arguments
does not affect the actual arguments Changes made to the formal argument t are local to block of called function, so when control back to calling function changes made vanish.
Example: void swap (int a , int b) /* called function */ { int t; t = a; a=b; b = t; } main( ) { int k = 50,m= 25; swap( k, m) ; / * calling function */ print (k, m); / * calling function */ } Output: 50, 25
Explanation:
int k= 50, m=25 ;
Means first two memory space are created k and m , store the values 50 and 25 respectively
swap (k,m);
When this function is calling the control goes to the called function.
void swap (int a , int b),
k and m values are assigned to the ‘a’ and ‘b’.
then a= 50 and b= 25 ,
After that control enters into the function a temporary memory space ‘t’ is created when int t is
executed.
t=a; Means the value of a is assigned to the t , then t= 50.
a=b; Here value of b is assigned to the a , then a= 25;
b=t; Again t value is assigned to the b , then b= 50;
after this control again enters into the main function and execute the print function print (k,m). it
returns the value 50 , 25.
NOTE:
Whatever change made in called function not affects the values in calling function.
Call by reference:
Here instead of passing value address or reference are passed. Function operators or address rather than values .Here formal arguments are the pointers to the actual arguments.
Example: #include void add(int *n); int main() { int num=2; printf(“\n The value of num before calling the function=%d”, num); add(&num); printf(“\n The value of num after calling the function = %d”, num); return 0; } void add(int *n) { *n=*n+10; printf(“\n The value of num in the called function = %d”, n); } Output: The value of num before calling the function=2 The value of num in the called function=20 The value of num after calling the function=20
NOTE:
In call by address mechanism whatever change made in called function affect the values in calling function.