C PROGRAMMING

call by value and call by reference demonstration

    

#include <stdio.h>  
void swap_call_by_value(int , int );
void swap_call_by_refrence(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %dn",a,b); // printing the value of a and b in main
swap_call_by_refrence(&a,&b);
printf("After using swapping function call by refrence values in main a = %d, b = %dn",a,b); // The values of actual parameters do change in call by reference, a = 10, b = 20
printf("nn");
swap_call_by_value(a,b);
printf("nn");
printf("values in main after calling function call by valuen a = %d b = %d ",a,b);

}
void swap_call_by_refrence (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
}

void swap_call_by_value (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("values in function of call by value a = %d b = %d n",a,b);
}

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button