Argument passing techniques :

Argument passing is a process of transferring data between the calling function and called function. In C , there are two ways by which arguments can be passed.

(a). Pass by value or Call by value

(b). Pass by address or pass by reference

(a). Pass by value or call by value :

The pass by value or call by value mechanism is the default mechanism for argument passing . When an argument(s) is passed by value, then a copy of values of arguments is made and passed to the corresponding formal arguments in the called function, not the variables itself. Since the formal arguments contain a copy of the value of actual arguments which are stored in separate memory location so any changes made to these are not reflected back to the actual argument. The changes made in the formal argument are local to the block which are lost once the control is returned back to the calling function.





Now let us consider a program to show how pass by value works

#include<stdio.h>

#include<conio.h>

void swap( )

{

int a=24, b=67

clrscr( );

printf("before calling (in main)");

printf("a=%d, b=%d, a,b);

swap(a,b) /*call by value*/

printf("/nAfter calling (in main)");

printf("a=%d, b=%d" ,a,b);

getch( );

}

void swap(int x, int y)

{

int temp;

temp= x;

x= y;

y= temp;

printf("/nAfter modification(in function)" );

printf("x= %d, y= %d" , x,y);

}





OUTPUT: before calling (in main)a=24, b=67

after modification (in function)x=67, y=24

after calling (in main)a=24, b=67





EXPLANATION: In the above program, we want to swap the two numbers. On execution, first the values of a and b are printed. On execution of the function call swap(a,b);, the separate copy of the value stored in a and b i.e. 10 and 25 are copied into the formal arguments x and y respectively. The function swap ( ), can access these copies only. The values of x and y are interchanged but the changes are made only to the copies of the values of the original arguments that were passed, therefore changes are not reflected back to the calling function main( )'s actual arguments a and b. This happens because when a function call is made, a separate memory is made for the formal arguments x and y and the values of the actual arguments a and b are copied to them.





The pass by value is mostly used when you want to avoid unintentional changes to the arguments. It is not suitable for conditions such as

When the values of the passed arguments must be changed. For example, if the user wants perform some operations in the interchanged values in the previous program then it cannot be done. When large amount of data wants to be passed as an argument. In that case it may take a large amount of execution time and space when the copy is made.





PASS BY ADDRESS:

The pass by address or call by reference method is another mechanism of passing arguments to a function. Unlike pass by value method where a copy of arguments is passed, in pass by address the formal arguments then those changes are reflected back to the actual arguments. The address of arguments is passed by preceding the address operator (&) with the name of variables whose value you want to modify. The corresponding formal arguments are preceded with asterisk(*) which acts as a pointer variable to store the address of the actual arguments.

Now let us consider a program to show how pass by address works.

Program to show how pass by address works?

#include<stdio.h>

#include<conio.h>

void swap(int *, int *);

main()

{

int a =24, b=67;

clrscr();

printf("before calling (in main) ");

printf("a= %d, b=%d

"a,b);

swap(&a,&b);

printf("after calling (in main) ");

printf("a= %d, b= %d

", a,b);

getch();

}

void swap(int *x, int *y)

{

int temp;

temp=*x;

*x=*y;

*y=temp;

printf("after modification (in function) ");

printf("x=%d, y= %d

", *x, *y);

}





OUTPUT:

before calling (in main) a=24, b=67

after modification (in function) x=67 y=24

after calling (in main) a=67, b=24





EXPLAINATION : The above program is used to interchange two numbers using pass by reference technique. On execution of the function call,

swap(&a, &b);

address of arguments a and b are passed to the formal arguments x and y respectively. As address are passed so formal arguments are declared to be pointers to int type. Any changes made to these formal arguments are reflected back to the actual arguments.





Differences between pass by value and pass by address:

1.In pass by value, When an arguments is passed by value , any modification made in the formal arguments in the called function are not reflected back in the actual arguments in the calling function in other hand in pass by address , when an arguments is passed by value , any modification made to the formal arguments passed by reference are reflected back in the actual arguments in the calling function.

2. In pass by value,It is default mechanism of argument passing in other hand in pass by address , user needs to specify this method of arguments passing.

3.In pass by value, It is not suitable for passing arguments of large size as separate copy consumes a lot of memory space in other hand in pass by address , it is suitable for passing argument of large sizes as not separate copy is made in the called function.

4.In pass by value, The value of the actual arguments are passed to the formal arguments in other hand in pass by address , address of the actual arguments is passed to the formal arguments by preceding address operator their names in function call



























