Monday, November 16, 2020

Parameter Passing - Call by Value, Call by Reference

In programming, argument refers to the variable passed to the function. There are two methods
to pass the arguments into the function in C language,

                         call by value and 

                        call by reference.

Call by value:

In call by value method, the value of the actual parameters is copied into the formal parameters.

In call by value method, the value of the actual parameter cannot be modified by the formal
parameter.

In call by value, different memory is allocated for actual and formal parameters since the value
of the actual parameter is copied into the formal parameter.

The actual parameter is the argument which is used in the function call whereas formal parameter is the
argument which is used in the function definition.

Example:

#include <stdio.h>  

void swap(int , int);      //prototype of the function   

int main()  

{  

    int a = 10;  

    int b = 20;   

    printf("Before swapping the values in main a = %d, b = %d\n",a,b); 

// printing the value of a and b in main  

    swap(a,b);  

    printf("After swapping values in main a = %d, b = %d\n",a,b); 

// The value of actual parameters do not change by changing the formal parameters in call 

// by value, a = 10, b = 20  

}  

void swap (int a, int b)  

{  

    int temp;   

    temp = a;  

    a=b;  

    b=temp;  

    printf("After swapping values in function a = %d, b = %d\n",a,b); 

 }  

Output:

Before swapping the values in main a = 10, b = 20

After swapping values in function a = 20, b = 10

After swapping values in main a = 10, b = 20

Call by Reference:

In call by reference, the address of the variable is passed into the function call as the actual
parameter.

The value of the actual parameters can be modified by changing the formal parameters since the address
of the actual parameters is passed.

In call by reference, the memory allocation is similar for both formal parameters and actual
parameters. All the operations in the function are performed on the value stored at the address of the actual parameters, and the modified value gets stored at the same address.

Example:

#include <stdio.h>  

void swap(int *, int *); //prototype of the function   

int main()  

{  

    int a = 10;  

    int b = 20;   

    printf("Before swapping the values in main a = %d, b = %d\n",a,b); 

// printing the value of a and b in main  

    swap(&a,&b);  

    printf("After swapping values in main a = %d, b = %d\n",a,b); 

// The values of actual parameters do change in call by reference, a = 10, b = 20  

}  

void swap (int *a, int *b)  

{  

    int temp;   

    temp = *a;  

    *a=*b;  

    *b=temp;  

    printf("After swapping values in function a = %d, b = %d\n",*a,*b); 

 }  

Output :

Before swapping the values in main a = 10, b = 20

After swapping values in function a = 20, b = 10

After swapping values in main a = 20, b = 10

0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top