write a program using call by value

Call by value program


                          call by value

Program


#include <stdio.h>
int increment(int var)
{
    var = var+1;
    return var;
}

int main()
{
   int num1=30;
   int num2 = increment(num1);
   printf("num1 value is: %d", num1);
   printf("\nnum2 value is: %d", num2);

   return 0;
}

Output:

num1 value is: 30
num2 value is: 31

Explanation


We passed the variable num1 while calling the method, but since we are calling the function using call by value method, only the value of num1 is copied to the formal parameter var. Thus change made to the var doesn’t reflect in the num1.

Post a Comment

0 Comments