# Learntofish's Blog

## Call by Value and Call by Reference

Posted by Ed on August 2, 2009

Today I’d like to talk about the difference between Call by Value and Call by Reference. I assume that you are familiar with pointers and references in C++, otherwise have a look at my blog posts on pointers and references.

We will write a function that squares a given integer variable. And we will do this in three different ways.

1) Call by Value
Let’s have a look at the following code:

```#include <iostream>
using namespace std;

//Define function squareVal
void squareVal(int x) {
x = x * x;
}

//main
int main()
{
int b = 8;
squareVal(b);
cout << "The value of b is " << b << endl;

system("PAUSE");
return 0;
}
&#91;/sourcecode&#93;
In line 5-7 we define the function <em>void squareVal(int x)</em>. Considering the argument this function expects an integer variable. In line 12-13 we declare the integer variable b and intialize it with 8. We then call the function by passing the variable b to it. What do you think will be the output of line 14? The output is
<code>The value of b is 8</code>
Obviously, the function squareVal is not able to change the value of b. This is because only the value of b is passed to the function. What actually happens when we write <em>squareVal(b)</em> in line 13 is:

void squareVal(int x = b) {
x = x * x;
}
```

So, the assignment int x = b takes place. Then x is squared within the function, but nothing happens to b. When passing only the value to a function we refer to it as Call by Value.

2) Call by Reference – using a pointer
Let’s have a look at the following code:

```#include <iostream>
using namespace std;

//Define function squarePtr
void squarePtr(int* x) {
*x = (*x) * (*x);
}

//main
int main()
{
int b = 8;
squarePtr(&b);
cout << "The value of b is " << b << endl;

system("PAUSE");
return 0;
}
&#91;/sourcecode&#93;
In line 5-7 we define the function <em>void squarePtr(int* x)</em>. Looking at its argument we know that this function expects an address since <em>int* x</em> is a <em>pointer</em>. In line 12-13 we declare the integer variable b and intialize it with 8. We then call the function by passing the address of b to it. What do you think will be the output of line 14? The output is
<code>The value of b is 64</code>
Unlike in the previous case the function here is able to change the value of b. This is because the address of b is passed to the function and the following happens in line 13 when we write squarePtr(&amp;b):

void squarePtr(int* x = &b) {
*x = (*x) * (*x);
}
```

The address of b is assigned to to the pointer. Then the value *x at which the pointer points is squared. Changing *x will also change the value of b. When we call a function by passing an address to it we talk about Call by Reference.

3) Call by Reference – using a reference
Let’s have a look at the following code:

```#include <iostream>
using namespace std;

//Define function squareRef
void squareRef(int& x) {
x = x * x;
}

//main
int main()
{
int b = 8;
squareRef(b);
cout << "The value of b is " << b << endl;

system("PAUSE");
return 0;
}
&#91;/sourcecode&#93;
In line 5-7 we define the function <em>void squareRef(int&amp; x)</em>. Looking at its argument we know that this function expects an integer variable since <em>int&amp; x</em> is a <em>reference</em>. In line 12-13 we declare the integer variable b and intialize it with 8. We then call the function by passing b to it. What do you think will be the output of line 14? The output is
<code>The value of b is 64</code>
Like in the previous case where we have used a pointer the function here is able to change the value of b. In line 13 when we write squareRef(b) the following happens:

void squareRef(int& x = b) {
x = x * x;
}
```

We therefore create a reference to b. Changing the value of the reference also changes the value of b. When we call a function by creating a reference we allude to it as Call by Reference.

Exercise
When using Call by Reference, do you think it is better to use a pointer or a reference?