Clear, practical technology insights About · Contact

Call the Function by Reference in C ++

Understand Call the Function by Reference in C ++ with clear explanations, practical examples, and useful tips. This updated guide covers the essential...

Published: 2 minutes read
Table of Contents

Call the Function by Reference in C ++ is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

The method of calling a function by reference of parameters passed to a function copies the address of a parameter into the official parameter. Within that function, the address used to access the parameter is actually used in calling the function. This means that changes are made with parameters that affect the passed parameters.

To pass values by reference, parameter pointers are passed to functions like any other value. Therefore, you need to declare the function parameters as pointer types as in the traodoi () function as follows, but change the value of the two integer variables pointed to by its parameters.

 // phan dinh nghia ham de trao doi cac gia tri. void traodoi ( int & x , int & y ) { int temp ; temp = x ; /* luu giu gia tri tai dia chi x */ x = y ; /* dat y vao trong x */ y = temp ; /* dat x vao trong y */ return ; } 

Now, we call the function traodoi () by passing the values by reference as follows:

 #include using namespace std ; // phan khai bao ham void traodoi ( int & x , int & y ); int main () { // Khai bao bien cuc bo: int a = 100 ; int b = 200 ; cout << "Truoc khi trao doi, gia tri cua a la: " << a << endl ; cout << "Truoc khi trao doi, gia tri cua b la: " << b << endl ; /* goi ham traodoi de trao doi cac gia tri boi su dung tham chieu bien.*/ traodoi ( a , b ); cout << "Sau khi trao doi, gia tri cua a la: " << a << endl ; cout << "Sau khi trao doi, gia tri cua b la: " << b << endl ; return 0 ; } 

You put the above function definition at the end of this code, then compile and run the above C ++ program will produce the following result:

This indicates that the changes reflect outside the function, unlike the function by the value that the changes do not reflect outside the function.

According to Tutorialspoint

Last lesson: Call the function by pointer in C ++

Next lesson: Number in C ++

Frequently Asked Questions

What is Call the Function by Reference in C ++?

The method of calling a function by reference of parameters passed to a function copies the address of a parameter into the official parameter.

Why is Call the Function by Reference in C ++ important?

A clear understanding of Call the Function by Reference in C ++ helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.

How should beginners approach Call the Function by Reference in C ++?

Start with the fundamental concepts, follow the examples step by step, and test each change in a safe environment before applying it to important systems or data.

Was this article helpful?

Your feedback helps us improve.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.