Call the function by value in C ++
The method calls the function by the value of the parameters passed to a function that copies the actual value of a parameter into the official argument of the function. In this case, changes made to the parameter inside the function have no effect on the parameter.
By default, C ++ Language uses the call function by value to pass parameters. Under the general sense, the code inside a function cannot change the parameter used to call the function. Suppose the definition of the function traodoi () is as follows:
// phan dinh nghia ham de trao doi cac gia tri. void traodoi ( int x , int y ) { int temp ; temp = x ; /* luu giu gia tri cua x */ x = y ; /* dat y vao trong x */ y = temp ; /* dat x vao trong y */ return ; }
Now we call the function traodoi () by passing true values like the following example:
#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 de trao doi cac gia tri. 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:
It indicates that there is no change in value even though they have been changed inside the function.
According to Tutorialspoint
Previous article: Function in C / C ++
Next lesson: Call the function by pointer in C ++
You should read it
- Do you know what programming language is?
- In the end, big universities realized that Java was a lousy language if used for introductory programming
- Overview of R language, install R on Windows and Linux
- Why should you learn Python programming language?
- The reason why C programming language is never outdated
- Test on C programming P6
- What is the first programming language in the world?
- What do you know about Smalltalk programming language?
- 10 programming languages booming today
- What do you know about programming language C # P1
- Variable parameter in C
- What is C programming language?
Maybe you are interested
Difference between function and formula in Excel
8 little-known Excel functions that can save you a lot of work
How to use the NORMDIST function in Excel - Function that returns the distribution in Excel
Date functions in Excel, DAY, WEEKDAY, MONTH
How to use the SUMIF function in Excel to calculate the sum based on conditions
How to use the Round function in Excel to round numbers and process data