Program to show Call by Value and Call by Reference in C Language

This article will illustrate the use of Call by Value and Call by Reference in C Programming language. C Language provide several features. Before, we talk about call by reference and call by value, let’s have a little talk about functions in C because both of them are used within the functions itself.

A function in c is a block of code that performs a certain task. It contains statements which are specified for a certain task. Functions makes programs easy to read and modify. Functions can be with or without parameters. Parameters are called arguments when they are passed in the main() function in other words arguments are jut parameters!For example, sum(), sum() function is without any argument, but how do I know that sum is a function? simply because it has round brackets (). sum(int a, int b) function contains two argument, both of int type!

Let’s move onto our real topic:

Call by Value – A Simple definition: A copy of values are passed as the argument to the function. So since value copy is passed, we call it call by value.

CODE:

#include<stdio.h>

void main(){
int a,b,res=0;
printf("Enter any two numbers: ");
scanf("%d%d",&a,&b);

printf("Before Swap of a = %d and b = %d",a,b);
swap(a,b); //actual parameters
printf("\nafter Swap a =%d and b=%d",a,b);
}
//below are formal parameters which are passed.
int swap(int x, int y){ 
int s;
s=x;
x=y;
y=s;
printf("\nINSIDE SWAP FUNC: Swapped values x =%d and y=%d",x,y);

}

OUTPUT:

call by value in C

Explanation:

The value is passed in swap function. There are two types of parameters: Actual and Formal. The Actual are the real values of the variables and formal ones are the once that will contain the actual values once in the function.

In the above program. The values for a and b are not changed because their copies have been stored in new variables which are x and y. So, in call by value, the copy is passed. We use call by value when we do not want to change the actual values of the variables, so we make a copy of them and use it in the program.

Therefore, after swapping, the value for a and B is same as before swapping, because in reality the swap function worked on x and y not a and b.



Call by Reference – A Simple Definition- It refers to the address of the actual variable. So If address of the variable is referred by another variable then it will modify the actual value. This does not pass copy of value. It passes the address reference of the variable.

CODE:

#include<stdio.h>

void main(){
int a,b,res=0;
printf("Enter any two numbers: ");
scanf("%d%d",&a,&b);

printf("Before Swap of a = %d and b = %d",a,b);
swap(&a,&b);
printf("\nafter Swap a =%d and b=%d",a,b);
}

int swap(int *x, int *y){
int s;
s=*x;
*x=*y;
*y=s;
printf("\nINSIDE SWAP FUNC: Swapped values x=%d and y=%d",x,y);

}

OUTPUT:

call by reference in C

Explanation:

Call by reference is done using pointers. Pointers are special variables in c which performs operations on the address/ location of the variables.

In the above program with the help of ‘&’ we passed the value for a and b in the swap function. swap(int *x,int *y), x and y grabs them addresses, ‘*’ is used to pull the values on those addresses. Once the values are pulled, swapping is performed on them and the output changes the actual values.

In the output above, the INSIDE swap line shows the values of x and y which are actually the address location of a and b. after swap changes the value of both a and b.

We use call by reference when we need to change the value of the variables, it is used for modification of actual values using their addresses.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: