call by values and call by reference functions..????? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

call by values and call by reference functions..?????

8th Jul 2017, 1:25 AM
Hassam Usmani
Hassam Usmani - avatar
3 Answers
+ 9
When you pass a variable to a function by value, the value of the variable is passed and assigned to a temporary local variable in the function. Any changes made to the value of the local variable does not affect the original variable outside of the function. When you pass a variable to a function by reference, the address of the variable, the reference of the variable is copied into the formal parameter. This reference will then be used to access the actual variable used in the call. Any changes made to the parameter affect the original variable.
8th Jul 2017, 1:44 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
int num = 3; int A(int byValue){ return ++byValue; } A(num); cout << num; // 3 ________________ int num = 3; int B(int& byReference){ return ++byReference; } B(num); cout << num; // 4 ______________________ int num = 3; int C(int* byReference){ return ++*byReference; } C(&num); cout << num; // 4 ______________________ int num = 3, *ptr = &num; int D(int* byReference){ return ++*byReference; } D(ptr); cout << num; // 4
8th Jul 2017, 1:39 AM
Andrés04_ve
Andrés04_ve - avatar
0
Yeah
8th Jul 2017, 1:28 AM
Matheus Michels
Matheus Michels - avatar