Passing Variables by Value


Passing Variables by Value

In C++, when non-array variable are passed as arguments to a function, the function cannot change those variables. This is because non-array variables are ordinarily passed by value. A copy of the argument's value is passed to the function and stored in a corresponding parameter. The scope of the function parameter variables is local to the function; changes made to those variables have no affect on the original arguments in the calling function.

For example, consider the following program:

#include <iostream>

using std::cout;
using std::endl;

void add_to_int(int);

int main()
{
    int num = 5;
   
    cout << "In main(), value of num is " << num << endl << endl;
   
    add_to_int(num);
   
    cout << "In main(), value of num is now " << num << endl;
   
    return 0;
}
   
void add_to_int(int num)
{
    cout << "In add_to_int(), value of num is " << num << endl << endl;
   
    num += 10;

    cout << "In add_to_int(), value of num is now " << num << endl << endl;
}

When this program is run, we get this output:

In main(), value of num is 5

In add_to_int(), value of num is 5

In add_to_int(), value of num is now 15

In main(), value of num is now 5

Although the add_to_int() function clearly changed num from 5 to 15, when control returned to main(), the change was somehow lost. Why did this happen?

A small alteration to the program will help illustrate what's going on. In addition to printing the value of the variable num, we'll also print its address.

#include <iostream>

using std::cout;
using std::endl;

void add_to_int(int);

int main()
{
   int num = 5;

   cout << "In main(), value of num is " << num << endl;
   cout << "In main(), address of num is " << &num << endl << endl;

   add_to_int(num);

   cout << "In main(), value of num is now " << num << endl;

   return 0;
}

void add_to_int(int num)
{
   cout << "In add_to_int(), value of num is " << num << endl;
   cout << "In add_to_int(), address of num is " << &num << endl << endl;

   num += 10;

   cout << "In add_to_int(), value of num is now " << num << endl << endl;
}

Now when we run the updated program, we get this output:

In main(), value of num is 5
In main(), address of num is 0x7ffc1932f58c

In add_to_int(), value of num is 5
In add_to_int(), address of num is 0x7ffc1932f55c

In add_to_int(), value of num is now 15

In main(), value of num is now 5

As you can see from this output, there is a variable num in main() and a variable num in add_to_int(), but they have different addresses, which means that they are not the same variable. The value of the variable num in main() was copied into the variable num in add_to_int(). The add_to_int() function changed its copy of num, but the variable num in main() was unaffected by that change.

Passing Objects by Value

C++ objects are non-array variables, so they are also passed by value by default.

For example, consider the following program:

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

void add_to_string(string);

int main()
{
    string s = "dog";

    cout << "In main(), value of s is " << s << endl << endl;

    add_to_string(s);

    cout << "In main(), value of s is now " << s << endl;

    return 0;
}

void add_to_string(string str)
{
    cout << "In add_to_string(), value of str is " << str << endl << endl;

    str = str + "fight";

    cout << "In add_to_string(), value of str is now " << str << endl << endl;
}

The use of a different name for the function parameter in add_to_string() (str) than the name of the variable that was passed to the function (s) makes it even clearer that they are not the same variable. Naturally, the output from the program reflects that:

In main(), value of s is dog

In add_to_string(), value of str is dog

In add_to_string(), value of str is now dogfight

In main(), value of s is now dog

Once again, any changes made to the copy of the string in add_to_string() do not affect the original variable that was passed to the function.

What if we want to have a function change the original variable in the calling routine? For non-array variables, we have two options:

  1. Pass the variable by address (using a pointer)
  2. Pass the variable by reference (using a C++ reference)