Pass by value vs pass by reference

When calling a function you can pass data to in 2 ways: pass it by value or pass it by reference.

Have a look at this piece of code:

void changeValue(int value)

int main()
	int myVariable=200;

our function changeValue accepts an integer by value and changes it to 500. Here is the output:

yes, myVariable’s value is still 200. Why? when we called the function changeValue we pass the value of myVariable which is 200. In the function, the variable value is created with the value we passed to the function, which is 200. So, modifying the value variable will not modify myVariable.

Let’s pass by reference now. Passing by reference means that we pass the variable’s address in memory and not the value. Let’s look at how a reference works first:

    int &ref = myVariable;

our new variable, ref, is a reference to an integer. References are declared with an ampersand(&) before it’s name, you can create one for any data type. In this our reference is called ref. On initialization, ref is assigned the address of myVariable. Yes, that is how you initialize a reference, it’s that easy. So modifying the reference will modify the variable it’s referencing. Hence, changing ref to 300 will change the value of myVariable to 300 as well. This is the output:

So let’s get to passing by reference when calling a function. Our new function now looks like this:

void changeValueByReference(int &value)

notice that the value parameter in this function is a reference. So when we call this function we’re passing the address of whatever argument passed to it. Lets use our kool new function now 😀 :


and the output is:

You can also pass by reference using pointers. Pointers are like reference with a few differences, i’ll explain them in another post. Here’s the function using pointers.

void changeValueByPointer(int *value)

and is used like this:


and the output is:

Look at some of the differences between using references and pointers. When calling the function the “address of” operator(&) is placed before the argument. When changing the value of the memory of address the pointer is pointing to an asterisk(*) is placed before the pointer. I’ll go into the how to use pointers in a different post.

So why on earth would you pass anything by reference to a function??

First reason:

You can only return a single value from a function via “return value”. By using references you can get multiple outputs from a function. Need an example?

int doTheMath(int value,int &half,int *quarter)
	half = value/2;
	*quarter = value/4;
	return 2*value;

and you use it by doing so:

    int half;
	int quarter;
	int times2;
	int number=8;
	times2 = doTheMath(number,half,&quarter);

	cout<<"Number: "<<number<<endl;
	cout<<"2X: "<<times2<<endl;
	cout<<"half: "<<half<<endl;
	cout<<"quarter: "<<quarter<<endl;

In the function, the first value passed to it is divided by 2, it’s also divided by 4 and multiplied by 2. That’s 3 values that needs to be outputted by the function. The first is assigned to the reference(half), the second to the pointer(quarter) and the third is returned from the function.

Here’s the output:

Another reason:

when you pass by value a new variable is created and the value of the variable you’re passing is copied to it. This is slow when passing large objects (instances of classes with a lot of data members), and very noticeable when your application requires alot of processing to be done in short periods of time. So passing by reference only passes the address of the variable, which is way faster :D.

So you now know the difference between passing by value and passing by reference.

Have fun coding 😀


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s