One of the key parts of any program is splitting parts off into reusable chunks called functions. Functions allow us to simplify our code and make it more reusable. While this tutorial is not going to take a deep dive into functions, it is important to understand a few basic ideas about how functions work.
A function is a block of code that can be called repeatedly to execute that code. Some functions operate independently, but many operate by passing information to the function and the function then returns data back to the main program.
Let’s look at a couple of examples of functions in C++.
In this tutorial, we are going to focus on the way that values are passed from the main program to the function. Let’s take one of the examples above to explain the terminology.
In the example above, the variables a
and b
as defined in the addNumbers
function are called parameters. a
and b
only exist inside the scope of the addNumbers
function.
The values of 11 and 13 as used when calling the addNumbers
function are called arguements. These values are sent to the addNumber
function.
By default, when argument values are sent to a function, the function creates a copy of these values to use as the function parameters. Any changes to the parameters in the function do not change the values of the arguments in the main program. This is called Pass By Value since we are passing the actual values of the arguments to the function.
Let’s take a look at an example of Pass By Value.
Notice in the example above how the values of a
and b
in the main function never change even though we swap the values in the function. Again, this is the idea of Pass By Value and it is the default way that C++ passes values to a function.
What if we actually wanted to swap the values in the main program? It can get a little complicated since we can only return one value. In C++ we can do this with what we call Pass By Reference.
In contrast to Pass By Value, Pass By Reference does not create a copy of the arguments in the function. Instead, it creates a reference to the original variables in the main program. Now, any changes made to the parameters in the function are also made to the variables in the main program.
To tell C++ to use Pass By Reference, we need to add an ampersand (&
) in front of the parameter in the function header. For example:
Let’s take a look at the program from above using Pass By Reference. Notice that the program is the exact same code with the one exception being the two &
in front of the parameters.
Take note in the example how the values of the main program are now updated once the function updates the values of the parameters. This is accomplished because we are passing a reference to the main program variables, not the actual values.
Pass By Reference can be useful in a couple of situations. First, as we see above, it can be helpful if we want to pass several variables to a function and make updates to those values in the function. Pass By Reference allows us to make updates without having to return a value.
Second, when dealing with large amounts of data (such as large vectors or arrays), Pass By Reference can help with memory management. When you Pass By Value, you are creating a copy of your data, which uses up more memory. Since Pass By Reference only uses a pointer back to the main data, you avoid having to duplicate the data set.