- C++ Function Call by Reference
- Calling a function with argument, by reference.
- Output-
- Program Analysis
- Output-
- Function call by value v/s call by reference
- C++ function call by reference
- C++ Functions - Pass By Reference
- Example
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- Function call by reference in C
C++ Function Call by Reference
- 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 using namespace std; int main() < // function prototype declaration void add10(int *); int a = 10; cout// function to add 10 to the int value passed to it void add10(int *i)
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 void return 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().
add10(&a); // add10 function is called
// 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.
// Calling a method with argument, by reference #include using namespace std; int main() < void swap_char(char *,char *); // method prototype declaration char a ='x'; char b ='y'; cout// method to swap characters void swap_char(char *c, char *d)
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.
C++ function call by reference
The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.
To pass the value by reference, argument reference is passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.
// function definition to swap the values. void swap(int &x, int &y) < int temp; temp = x; /* save the value at address x */ x = y; /* put y into x */ y = temp; /* put x into y */ return; >
For now, let us call the function swap() by passing values by reference as in the following example −
#include using namespace std; // function declaration void swap(int &x, int &y); int main () < // local variable declaration: int a = 100; int b = 200; cout << "Before swap, value of a :" << a << endl; cout << "Before swap, value of b :" << b << endl; /* calling a function to swap the values using variable reference.*/ swap(a, b); cout
When the above code is put together in a file, compiled and executed, it produces the following result −
Before swap, value of a :100 Before swap, value of b :200 After swap, value of a :200 After swap, value of b :100
C++ Functions - Pass By Reference
In the examples from the previous page, we used normal variables when we passed parameters to a function. You can also pass a reference to the function. This can be useful when you need to change the value of the arguments:
Example
int main() int firstNum = 10;
int secondNum = 20;
// Call the function, which will change the values of firstNum and secondNum
swapNums(firstNum, secondNum);
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
Function call by reference in C
The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.
To pass a value by reference, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to, by their arguments.
/* function definition to swap the values */ void swap(int *x, int *y) < int temp; temp = *x; /* save the value at address x */ *x = *y; /* put y into x */ *y = temp; /* put temp into y */ return; >
Let us now call the function swap() by passing values by reference as in the following example −
#include int main () < /* local variable definition */ int a = 100; int b = 200; printf("Before swap, value of a : %d\n", a ); printf("Before swap, value of b : %d\n", b ); /* calling a function to swap the values */ swap(&a, &b); printf("After swap, value of a : %d\n", a ); printf("After swap, value of b : %d\n", b ); return 0; >void swap(int *x, int *y) < int temp; temp = *x; /* save the value of x */ *x = *y; /* put y into x */ *y = temp; /* put temp into y */ return; >
Let us put the above code in a single C file, compile and execute it, to produce the following result −
Before swap, value of a : 100 Before swap, value of b : 200 After swap, value of a : 200 After swap, value of b : 100
It shows that the change has reflected outside the function as well, unlike call by value where the changes do not reflect outside the function.