Topics

6/recent/ticker-posts

Call by value in c language

 Call by Value:


Definition:   It is a calling function by passing value of the variable.


Call by value:

Eg:

 

#include<iostream>

int main()

{

int a=10,b=20;

void display(int, int);

display(a,b);

return 0;

} 

void display(int x, int y)

{

printf("\n %d  %d ",x,y);

}


Swapping two variables using call by value function:


#include<iostream>

int main()

{

int a=10,b=20;

void swap(int , int );

            printf("\b Before swapping ");

            printf("\n %d %d",a,b);

            swap(a,b);

            printf("\n After swapping");

            printf("\n %d %d",a,b);

return 0;

 }

 

 void swap(int x, int y)

 {

   int temp;

            temp=x;

            x=y;

            y=temp;

 }

 

 


O/p:

            Before swapping

10        20

After  swapping

10        20

Reason:

 

In the above example, swap() function is call by value, its arguments local variables. Hence local copies creates for arguments x and y.

 

Swapping takes place on local copies not on actual variables  a and b.

 

In call  by value function, the changes made on arguments will not reflect on actual variables ( passing variable);

 Note: 

Till here we have seen some basics of c language from but from now we will see file management from the next post. So get ready!!

Post a Comment

0 Comments