Flag This Hub

Call by Value and Call by Reference in C Programming

By


Call by value and call by reference is the most confusing concept among new C language programmer. I also struggled with this, the reason may be my teacher is not explaining it in simple words or I was dumb. Whatever the reason is; here I will try to explain it in simple word. Let’s start with classic example, classic example means whenever people talk about call by value or call by reference, they cite this example i.e. swapping of two variable.

Call by Value and Call by Reference

#include "stdio.h"
#include "conio.h"

void callByValue(int, int);
void callByReference(int *, int *);

int main()
{
	int x=10, y=20;
	clrscr();
	printf("Value of x = %d and y = %d.\n", x,y);

	callByValue(x, y);
	printf("\nCall By Value function call...\n");
	printf("Value of x = %d and y = %d.\n", x,y);

	callByReference(&x, &y);
	printf("\nCall By Reference function call...\n");
	printf("Value of x = %d and y = %d.\n", x,y);

	getch();
	return 0;
}

void callByValue(int x, int y)
{
	int temp;
	temp = x;
	x = y;
	y = temp;
}

void callByReference(int *x, int *y)
{
	int temp;
	temp = *x;
	*x = *y;
	*y = temp;
}
C Programming Language (2nd Edition)
Amazon Price: $35.64
List Price: $67.00
C (Vintage)
Amazon Price: $1.03
List Price: $15.00
Nature's Way Vitamin C 1000 with Rose Hips, 250 Capsules
Amazon Price: $11.76
List Price: $26.99
C++ Primer Plus (6th Edition) (Developer's Library)
Amazon Price: $31.98
List Price: $59.99
Beginning C++ Through Game Programming
Amazon Price: $21.29
List Price: $34.99
C++ Programming in Easy Steps
Amazon Price: $8.25
List Price: $14.99

In above call by value and call by reference C program example we have two functions; “callByValue()” and “callByReference()” to demonstrate respective logic.

Let’s start with “callByValue()” function, this function (line no. 25-31) does swapping of variable using temp variable, we have to pass two integer type data as arguments to this function.

The “callByReference()” function (line no. 33-39) also does swapping of variable using temp variable, but we have to pass two integer type pointer as arguments of this function. To reduce confusion; I kept variable name and logic same in both function except some extra asterisk (*) in “callByReference()” function.

We have declared two integer variable in line no. 9. We will pass those two variables to both function and will try to swap value stored in that. First we are calling function “callByValue()” (line no. 13) and we are passing those two integer value. In line no. 15 we are displaying the value of variables (x and y) after swapping. I know that swapping didn’t happen when we called “callByValue()” function.

In line no. 17 we are calling function “callByReference ()” and here we are passing address ofthose two integer variable. In line no. 19 we are displaying the value of variables (x and y). As you can see value of variables (x and y) is successfully swapped.

You must be thinking why this swapping logic worked for “callByReference()” function but not for “callByValue()” function.

Why swapping logic didn’t work for “Call by Value”?

When we called function “callByValue()” in line no. 13 that time we passed value of “x” and “y”, which means we passed copy of “x” and “y” variable value not the actual “x” and “y”. Now you understood why this method of calling function is called “Call by value”? Let me tell you one interesting point about this function, when you will print value of “x” and “y” variable inside this function it will show you the swapped value. But as I told you this logic works on copy of value so those changes will not reflect in “main()” function. See below example and output.

But in case of “callByReference()” function we passed address of “x” and “y” variables (see line no. 17 carefully there we are passing ampersand ‘&’ along with variable name), which means actual location of “x” and “y” variables. So in this function we swapped value which was stored in “x” and “y” location rather than copy of value. As this logic works on address (reference) of variable so this logic is known as “Call by Reference”.

#include "stdio.h"
#include "conio.h"

void callByValue(int, int);
void callByReference(int *, int *);

int main()
{
	int x=10, y=20;
	clrscr();
	printf("Value of x = %d and y = %d.\n", x,y);

	printf("\nCall By Value function call...\n");
	callByValue(x, y);
	printf("Value of x = %d and y = %d.\n", x,y);

	printf("\nCall By Reference function call...\n");
	callByReference(&x, &y);
	printf("Value of x = %d and y = %d.\n", x,y);

	getch();
	return 0;
}

void callByValue(int x, int y)
{
	int temp;
	temp = x;
	x = y;
	y = temp;

	printf("Value of x = %d and y = %d inside callByValue function.\n", x,y);
}

void callByReference(int *x, int *y)
{
	int temp;
	temp = *x;
	*x = *y;
	*y = temp;
}

Your opinion

Did this help you to learn Call by Value and Call by Reference concept?

  • Yes
  • No
  • Sort of
See results without voting

Comments

sandeep 3 months ago

C made simple

sudheer 3 months ago

help somthing

navi 2 months ago

helpful, thanks sir

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working