Function call: Pass by Value

Here, is the program of function call pass by value-

#include<stdio.h>

#include<conio.h>

void interchange(int number 1, int number 2)

{

int temp;

temp=number 1;

number 1= number 2;

number 2= temp;

}

int main()

{

int num 1=50, num 2=70;

interchange(num 1, num 2);

printf(“\n Number 1: %d”,num 1);

printf(“\n Number 2 : %d”,num 2);

return 0;

}

Output :

Number 1: 50

Number 2: 70

  • While passing parameters using call by value, xerox copy of original parameter is created and passed to the called function.
  • Any update made inside method will not affect the original value of variable in calling function.
  • In the above example num 1 and num 2 are the original values and xerox copy of these values is passed to the function and these values are copied into number 1, number 2 variable of sum function respectively.
  • As their scope is limited to only function so they cannot alter the values inside main function.

Advantages of Pass by Value:

  1. When arguments are passed by value , the called function creates new variables of the same data type as the arguments passed to it.
  2. The values of the arguments passed by the calling function are copied into the newly created variables.
  3. Values of the variables in the calling functions remain unaffected when the arguments are passed using the pass by value.

Disadvantage of Pass by Value :

  1. If the function has to pass a large amount of data may fill up whole space very quickly.
  2. It is a slow process.

Leave a comment

Design a site like this with WordPress.com
Get started