call by value and call by reference

What is call by value and call by reference in ‘C’ Programming

A function is a self contained block of statements which are kept together to perform a specific task in related manner. C  language is said to be the combinations of functions. Every C program is required to use at least one function. The main() function is the necessity of C program because

1. The C program execution requires it as Recognisation point for starting.
2. We use main( ) function to hold all the programming code for solving a specific problem.

The main advantages of using the functions in C programs are :

1.  It helps in easy program design.

2. The functions make the program modular and a module is easy to understand because of small length.

3. The use of function also facilitate reusability of programming code in the same program or different programs because the code to be reused is referred to as functions.

Call By Value :

When a single or more values are passed to a function via an actual arguments. The value of actual arguments are copied into the function. Therefore, the value of a corresponding formal argument can be altered within the function, but the value of the actual arguments within the calling routine will not change. This procedure for calling the function is known as call by value.

Example :

main()
{ int a = 2, b = 3 ;
printf(“a = %d, b = %d from main before calling”, a, b);
value(a, b);
printf(“a = %d, b = %d from main after calling”, a, b);
value (int a, int b)
{ a = a + 3 ; b = b + 3 ;
printf(“a = %d, b = %d from modular function”, a, b);
}

Output:

a = 2, b = 3 from main before calling a = 5, b = 6 from modular
function a = 2, b = 3 from main after calling

Call By Reference :

When an argument is passed by reference i.e. when a pointer is passed to function, the address of a data argument is passed to the function.

Example :

main()
{
int a = 2, b = 3;
printf(“a = %d, b = %d from main before calling”, a, b); value (&a,
&b);
printf(“a = %d, b = %d from main after calling”, a, b);
value (int *p, int *q)
{
*P = *P+3;
*q = *q+3;
printf(“a = %d, b = %d from modular function”, *p, *q);

Output:

a = 2, b = 3 from main before calling
a = 5, b = 6 from modular function
a = 5, b = 6 from main after calling

Hello Viewers,
welcome to the Allinonetuts a Technical tutorial and Programming Blog Maintained by Vikas Kardam, a Web Developer and Blogger From Delhi, India.
Call by value and call by reference was last modified: April 13th, 2017 by Vikas Kardam

Leave a Reply

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