C++ Fundamentals
3 Ways of Passing Arguments in C++
Know Your Arguments
There are only three ways one can pass arguments to a function:
- Pass By Value
- Pass By Reference
- Pass By Address
All the 3 ways will be discussed in detail in this article.
Before moving on let us look into the Actual Parameters and Formal Parameters.
- Actual Parameters (also known as arguments) are the values that are being passed to the function.
- Formal Parameters are the variables that are defined in the function definition.
Pass By Value
In Pass By Value, the value of an actual parameter is copied to the formal parameters. The changes made to the formal parameters inside the function definition will not be reflected in the variables of the actual parameters. In C++ by default the arguments are passed by value.
Output for this code is:
Before the function call: 1 2 3 4 5
After the function call: 1 2 3 4 5
When the control is transferred from the main function to the multiply_array function, new memory location is made for the formal parameter and the value of the actual parameter is copied into that. On printing the address of the array in the main and the multiply_array function we obtain different values. Thus it is confirmed that the copy of the actual parameter variable is passed into the formal parameter.
void multiply_array(vector<int> arr, int mul_value) {
cout << "array address in multiply_array function: " << &arr <<
endl;
}
int main() {
vector<int> arr = {1, 2, 3, 4, 5};
cout << "array address in main function: " << &arr << endl;
multiply_array(arr, 5);
return 0;
}
Pass by value uses more memory as for the same variable, 2 memory locations are made.
Pass By Reference
In Pass By Reference, the reference of an actual parameter is copied into the formal parameters. So in the function the reference is used to access the actual value of the argument. Therefore any changes made inside the function will be reflected in the variables of actual parameters. To pass a variable by reference & symbol is used while catching the formal argument. See the example below to have a clearer picture.
Output for this code is:
Before the function call: 1 2 3 4 5
After the function call: 5 10 15 20 25
When the control is transferred from the main function to the multiply_array function, there is no new memory location created for the formal parameters. Rather the formal parameter is used to refer to the memory location of the actual parameter. On printing the address of the array in both the functions we get the same result, which confirms the previous statement.
void multiply_array(vector<int> &arr, int mul_value) {
cout << "array address in multiply_array function: " << &arr <<
endl;
}
int main() {
vector<int> arr = {1, 2, 3, 4, 5};
cout << "array address in main function: " << &arr << endl;
multiply_array(arr, 5);
return 0;
}
Pass by reference is saves memory as the reference of the variable is copied.
Pass By Address
In Pass By Address also known as Pass By Pointer, copies the address of the actual parameters in the formal parameters. In the function, the address is used to access the actual argument. Since the address of a variable is used, the changes made to the formal parameters inside the function will affect in the actual parameters. To pass the variable by address & symbol is used while passing the actual parameter and * symbol is used while catching the formal parameter.
Output for this code is:
Before the function call: 1 2 3 4 5
After the function call: 5 10 15 20 25
When the control is transferred from the main function to the multiply_array function, new memory location is created for storing the value of the formal argument pointers. On printing the address of the pointer in the multiply_array function and address of the array in the main function we get different values, thus confirming the previous statement.
void multiply_array(vector<int> *arr, int mul_value) {
cout << "array address in multiply_array function: " << &arr <<
endl;
}
int main() {
vector<int> arr = {1, 2, 3, 4, 5};
cout << "array address in main function: " << &arr << endl;
multiply_array(&arr, 5);
return 0;
}
In conclusion it can be said that pass by reference and pass by address is more efficient (both in time and space) than pass by value. Thanks for reading the article. Hope this was helpful. Connect with me on LinkedIn.