ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Call by Value and Call by Reference

Updated on June 18, 2012
Source

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;
}
Source

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;
}
Source

Your opinion

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

See results

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      sandeep 5 years ago

      C made simple

    • profile image

      sudheer 5 years ago

      help something

    • profile image

      navi 5 years ago

      helpful, thanks sir

    • profile image

      aditya.world09@gmail.com 4 years ago

      Many Many thnx sir............

    • profile image

      Pankaj 4 years ago

      You are great.......

    • profile image

      Raheel 4 years ago

      Thanx sir, it helped a lot

    • profile image

      Ragini 4 years ago

      Thanks sir

    • profile image

      kevi 4 years ago

      thnks very much

    • profile image

      sunil 4 years ago

      really helpfull ......

    • profile image

      vikashvverma 3 years ago

      According to K&R , C uses only call by value and what you are explaining as call by reference is essentially call by value. This is what we call call by reference

      int swap(int &a);//call by refrence

      www.programminggeek.in

    • profile image

      priyanka sharma 3 years ago

      thanks

    • profile image

      harshita 3 years ago

      Really its very useful to me

    • profile image

      vickey 2 years ago

      thanks sir it's really very helpful

    • profile image

      dipali 22 months ago

      thank its really very very helpful

    Click to Rate This Article