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. 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:

Call the function by reference in C ++ Picture 1

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 ++

5 ★ | 1 Vote

May be interested

  • The OFFSET function returns a reference in ExcelThe OFFSET function returns a reference in Excel
    the offset () function returns a reference to a range, a cell or a range of cells with the number of rows and columns specified by you. the return reference can be a cell or a range of cells that you can specify the number of rows and columns to return.
  • How to use Lookup function in ExcelHow to use Lookup function in Excel
    lookup in excel is a reference function, the most commonly used search function in excel.
  • Callable () function in PythonCallable () function in Python
    callable () in python checks whether objects can be called. if the object is allowed to call, the function returns true, otherwise it returns false. how does the callable () function have the syntax, what parameters do we have, we will learn in this section.
  • How to use Vlookup function in Excel to search and referenceHow to use Vlookup function in Excel to search and reference
    are you new to excel and find it difficult to use the vlookup function? this article will share how to look up and compare information quickly using vlookup, please refer to it and apply it immediately.
  • Function realloc () in CFunction realloc () in C
    the realloc () function attempts to recover the memory block pointed to by the ptr pointer that was previously allocated with a call to malloc or calloc.
  • Free () function in CFree () function in C
    the void free function (void * ptr) frees the previously allocated memory by a call to calloc, malloc, or realloc
  • How to create a quick reply to a call on iPhoneHow to create a quick reply to a call on iPhone
    on the iphone there is a quick reply function by texting calls that you cannot listen to, with the option to edit the message content.
  • Shell functionsShell functions
    functions allow you to streamline an overall feature of a script into a smaller and more logical part that can perform the same function whenever it is needed through the function call.
  • Offset function in Excel - Usage and examplesOffset function in Excel - Usage and examples
    offset function in excel - usage and examples. the offset function in excel is a function of the reference and search function group, the following article will describe the syntax, usage and examples of the offset function in excel, please refer to to understand more about the offset function.
  • Anti-spam call with 'cricket': Why not?Anti-spam call with 'cricket': Why not?
    in addition to the services provided by carriers, phone companies have designed anti-spam features for their users when designing.