Passing Parameters

2021-03-15
3 min read

Frequently, you need to pass values or variables to a subroutine. The exact form of the subroutine interface varies with the programming language, but will be similar to the examples below:

procedure subroutineName (parameter1, parameter2, ...)
{
    // Do something
    return;
}
function subroutineName (parameter1, parameter2, ...)
{
    // Do something
    return value;
}

In some programming languages, parameters may be passed in different ways. If a parameter is passed by value, its actual value is passed to the subroutine, where it is treated as a local variable. Changing a parameter inside the subroutine will not affect its value outside the subroutine. If a parameter is passed by reference, its memory address is passed to the subroutine, where it’s original value can be accessed and changed by the subroutine. Changing a parameter inside the subroutine will affect its value outside the subroutine.

Here is an example of passing by value and passing by reference. Look through the code and guess what will be output. Then run the online demo and see what will happen.

#include <iostream>
using namespace std;

void pass_by_value(int value)
{
    cout << "The value in pass_by_value function is " << value << endl;
    cout << "The address in pass_by_value function is " << &value << endl;
    cout << "Add the value by one" << endl;
    value += 1;
    cout << "Now, the value in pass_by_value function is " << value << endl;
    cout << "Now, the address in pass_by_value function is " << &value << endl;
}

void pass_by_ref(int &value)
{
    cout << "The value in pass_by_ref function is " << value << endl;
    cout << "The address in pass_by_ref function is " << &value << endl;
    cout << "Add the value by one" << endl;
    value += 1;
    cout << "Now, the value in pass_by_ref function is " << value << endl;
    cout << "Now, the address in pass_by_ref function is " << &value << endl;
}

int main()
{
    int value = 1;
    cout << "The value in main function is " << value << endl;
    cout << "The address in main function is " << &value << endl;
    pass_by_value(value);
    cout << "The value in main function is " << value << endl;
    cout << "The address in main function is " << &value << endl;
    pass_by_ref(value);
    cout << "The value in main function is " << value << endl;
    cout << "The address in main function is " << &value << endl;
    return 0;
}

Run this code online: https://onlinegdb.com/DGTTUozWF

Here is a sample output. Yours run might have a different memory address, but the basic idea is the same.

The value in main function is 1
The address in main function is 0x7fff818056bc
The value in pass_by_value function is 1
The address in pass_by_value function is 0x7fff8180569c
Add the value by one
Now, the value in pass_by_value function is 2
Now, the address in pass_by_value function is 0x7fff8180569c
The value in main function is 1
The address in main function is 0x7fff818056bc
The value in pass_by_ref function is 1
The address in pass_by_ref function is 0x7fff818056bc
Add the value by one
Now, the value in pass_by_ref function is 2
Now, the address in pass_by_ref function is 0x7fff818056bc
The value in main function is 2
The address in main function is 0x7fff818056bc

You can see that for pass_by_value, the variable inside the subroutine has a different memory address with the variable in the main function, and change the value inside the subroutine will not affect the original value. For pass_by_ref, the variable inside the subroutine has the same memory address with the variable in the main function, and change the value inside the subroutine will affect the original value.