In one of our previous articles, we have explained the first technique about how to call a function that has arguments, by passing it arguments by value.
Now, we are going to explain the second technique by which
we could call a function that has arguments and pass the arguments to this function by reference i.e. by address.
Before we begin understanding the second technique, we will have to understand a little but about a very important feature of C language - Pointers.
Pointers play a major role in understanding function call by reference. Hence, for those who are
not aware of Pointers, please read our article Pointers in C++ before continuing ahead.
Calling a function with argument, by reference.
In the upcoming example, we are going to create a function named add10 which adds 10 to the variable of int type passed to it using pointers.
// Calling a function with argument, by reference
#include<iostream>
using namespace std;
int main()
{
// function prototype declaration
void add10(int *);
int a = 10;
cout<< "The value in a is : " << a << "\n";
add10(&a); // calling the add10 function
cout<< "After the add10 function is called " << "\n";
cout<< "The value in a is : " << a << "\n";
return 0;
}
// function to add 10 to the int value passed to it
void add10(int *i)
{
*i = *i + 10;
}
Output-
The value in a is : 10
After the add10 function is called
The value in a is : 20
Program Analysis
Declaring function prototype -
In the above mentioned example, we have created a function named add10 and have declared its prototype as -
/* function prototype declaration*/
void add10(int *);
The prototype of this function is declared with a voidreturn type, because this method will not return any value when it is called.
This function will be passed the address of an int value when it is called.
Hence we have specified a pointer that points to a value of int type i.e. int *, within the parenthesis().
Calling the function - We have called the add10 method by passing the address of value of an int
variable named a as an argument, by using the "address of" operator i.e. &.
The int variable a has a value 10 and its address is referred as an "actual argument" of the function add10, when it is called.
add10(&a); // add10 function is called
Function definition - When the add10 function is called, it receives the address of the variable(a) passed to it
and this address is copied into pointer variable
named i, which is used to store the address of an int variable, declared in the definition of add10 function.
// add10 function is defined
void add10(int *i)
{
*i = *i + 10; // using * to accessing the value at address
}
In the function definition, the pointer variable i can also be referred as a "formal argument" of add10 function.
This function, uses the * operator which stands for "value at address", which when used with a pointer variable i.e. i, allows us to access the value at an address in the memory location, pointed by i variable.
Eventually, the add10 function adds 10 to the value contained in i, which is reflected when the value of variable a is printed again on the console after calling the add10 function.
This proves that when we call a function that has arguments, by passing it arguments by reference(using pointers), the changes made on the values contained in the formal arguments within
the function definition
do affect the original value contained in actual arguments.
Advertisement
Another example of calling a function by reference
In the upcoming example, we are going to create a function named swap_char which accepts two char arguments and successfully swaps the value contained in these two char variables
passed to it, because we have called the function and have passed it arguments by reference using pointers. Let's see the code.
// Calling a method with argument, by reference
#include<iostream>
using namespace std;
int main()
{
void swap_char(char *,char *); // method prototype declaration
char a ='x';
char b ='y';
cout<< "The character value in a is : " <<a << "\n";
cout<< "The character value in b is : " <<b << "\n";
swap_char(&a,&b); // calling the swap_char method
cout<< "After the swap_char method is called " << "\n";
cout<< "The character value in a is : " <<a << "\n";
cout<< "The character value in b is : " <<b << "\n";
return 0;
}
// method to swap characters
void swap_char(char *c, char *d)
{
char e;
e = *c;
*c = *d;
*d = e;
}
Output-
The character value in a is : x
The character value in b is : y
After the exchange function is called
The character value in a is : y
The character value in b is : x
Function call by value v/s call by reference
On calling a function that has arguments, by passing it arguments by value, the values in the actual arguments passed to the
it are only copied in its formal arguments. Hence, the changes made on the values contained in the formal arguments in the function definition is not reflected
and does not affect the original value contained in actual arguments.
On calling a function that has arguments, by passing it arguments by reference, the address of original values in the actual arguments is passed to the
formal arguments in the function definition. Hence, the changes made on the values by accessing their address
contained in the formal arguments within the function definition
does affect the original value contained in actual arguments.