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:
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 ++
You should read it
- ISREF function - The function returns True if the value is a valid reference value in Excel
- Call the function by pointer in C ++
- Call the function by value in C ++
- Reference in C ++
- Load the operator stack to call the function () in C ++
- Anonymous method in C #
- INDIRECT function in Excel - How to use INDIRECT function and examples using INDIRECT function
- The OFFSET function returns a reference in Excel
May be interested
- The OFFSET function returns a reference in Excelthe 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 Excellookup in excel is a reference function, the most commonly used search function in excel.
- Callable () function in Pythoncallable () 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 referenceare 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 Cthe 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 Cthe 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 iPhoneon 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 functionsfunctions 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. 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?in addition to the services provided by carriers, phone companies have designed anti-spam features for their users when designing.