Thursday, March 14, 2013

Program to Implement Call By Value (CBV)

When we passes any argument to a function, a copy of the arguments is passed to the function.
If we change value of the arguments in the function it will not affect the original arguments which are passed to the function. This is called Call by Value.

The arguments which are passed to a function are known as Actual arguments and the arguments declared in function definition is known as Formal arguments.
In call by value Formal arguments contains copy of actual arguments.
In  following example "a" and "b" are the Actual arguments which are passed to the swap( ) function and "x" and "y" are the Formal arguments .

This program shows the how call by value works-
This is a simple swapping program.Swapping means interchanging. Here in this program a function named "swap( )" is derived for swapping of two variables. We are passing two values as "a" and "b". Check the value of "a" and "b" before calling the swap( ) function. And after execution of swap( ) function, again check the values of "a" and "b". You will see that both values are same. This is because when call by value is performed any changes made to value of Formal arguments does not affect the value of Actual arguments.

Powered by Blogger.