PASSING PARAMETERS TO FUNCTIONS :
The called function receives the information from the calling function through the parameters. The
variables used while invoking the calling function are called actual parameters and the variables used in
the function header of the called function are called formal parameters.
C provides two mechanisms to pass parameters to a function.
1 : Pass by value (OR) Call by value.
2 : Pass by reference (OR) Call by Reference.
1 : Pass by value (OR) Call by value : When a function is called with actual parameters, the values of actual
parameters are copied into formal parameters. If the values of the formal parametes changes in the function, the values of the actual parameters are not changed. This way of passing parameters is called pass by value or call
by value.
Ex :
#include<stdio.h>
#include<conio.h>
void swap(int a,int j);
void main()
{
int i,j;
printf("Enter i and j values:");
scanf("%d%d",&i,&j);
printf("Before swapping:%d%d\n",i,j);
swap(i,j);
printf("After swapping:%d%d\n",i,j);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
}
2 : Pass by reference (OR) Call by Reference :
In pass by reference, a function is called with addresses of actual parameters. In the function header, the
formal parameters receive the addresses of actual parameters. Now the formal parameters do not contain values,
instead they contain addresses. Any variable if it contains an address, it is called a pointer variable. Using
pointer variables, the values of the actual parameters can be changed. This way of passing parameters is called
call by reference or pass by reference.
Ex : #include<stdio.h>
#include<conio.h>
void swap(int a,int j);
void main()
{
int i,j;
printf("Enter i and j values:");
scanf("%d%d",&i,&j);
printf("Before swapping:%d%d\n",i,j);
swap(&i ,&j);
printf("After swapping:%d%d\n",i,j);
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}