Parameter Passing Technique
The parameter passing mechanism refers to the actions taken regarding the parameters when a function is invoked. There are two parameter passing techniques available in C.They are:
1) Call By Value
2) Call By Refernce
Call by value:
Whenever a portion of the program invokes a function with formal arguments, control will be transferred from the main to the calling function and the value of the actual argument is copied to the function. within the function,the actual value copied from the calling portion of the program may be altered or changed. when the control is transferred back from the function to the calling portion of the program, the altered values are not transferred back. This type of passing formal arguments to a function is technically known as "Call by value".
Ex: A program to exchange the contents of two variables using
a call by value.
#include
main()
{
int x,y;
void swap(int,int)
x=10;
y=20;
printf("values before swap()\n");
printf("x=%d and y=%d",x,y);
swap(x,y); /*call by value*/
printf("values after swap()\n");
printf("x=%d and y=%d ",x,y);
}
void swap(int x ,int y) /*values will not be swapped */
{
int temp;
temp=x;
x=y;
y=temp;
}
Output of the above program:
values before swap()
x=10 and y=20
values after swap()
x=10 and y=20
Call by Reference:
When a function is called by a portion of program, the address of the actual arguments are copied onto the formal arguments, though they may be referred by different variable names.The content of the variables that are altered with in the function block are returned to the calling portion of a
program in the altered form itself, as the formal and actual arguments are referring the same memory location or address. This is technically known as call by reference or call by
address.
Ex: A program to exchange the contents of two variables using
a call by reference.
#include
main()
{
int x, y;
void swap(int *x, int *y);
x=10; y=20;
printf("values before swap() \n");
printf("x=%d and y=%d \n",x,y);
swap(&x,&y); /* call by reference */
printf("values after swap() \n");
printf("x=%d and y=%d \n",x,y);
}
void swap(int *x ,int *y) /*values will be swapped */
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
Output of the above program:
values before swap()
x=10 and y=20
values after swap()
x=20 and y=10
No comments:
Post a Comment