Function Call by Value in C Programming
Function Call by Value in C Programming
When we call a function by passing the addresses of actual parameters then this way of calling the function is
known as call by reference. In call by reference, the operation performed on formal parameters, affects the
value of actual parameters because all the operations performed on the value stored in the address of actual
parameters. It may sound confusing first but the following example would clear your doubts.
Example:
#include <stdio.h>
void increment(int *var)
{
*var = *var+1; Output:
} Value of num is: 21
int main()
{
int num=20;
increment(&num);
printf("Value of num is: %d", num);
return 0;
}
26-11-2019 SNS College of Technology Ms.J.Vasuki,AP/IT 3
Swapping numbers using Function Call by Value
#include <stdio.h>
void swapnum( int var1, int var2 )
{
int tempnum ;
tempnum = var1 ; Output:
var1 = var2 ;
var2 = tempnum ; Before swapping: 35, 45
} After swapping: 35, 45
int main( )
{
int num1 = 35, num2 = 45 ;
printf("Before swapping: %d, %d", num1, num2);
swapnum(num1, num2);
printf("\nAfter swapping: %d, %d", num1, num2);
}
#include
void swapnum ( int *var1, int *var2 )
{
int tempnum ;
tempnum = *var1 ;
*var1 = *var2 ; Output:
*var2 = tempnum ;
} Before swapping:
int main( ) num1 value is 35
{ num2 value is 45
int num1 = 35, num2 = 45 ; After swapping:
printf("Before swapping:"); num1 value is 45
printf("\nnum1 value is %d", num1); num2 value is 35
printf("\nnum2 value is %d", num2);
swapnum( &num1, &num2 );
printf("\nAfter swapping:");
printf("\nnum1 value is %d", num1);
printf("\nnum2 value is %d", num2);
return 0;
}