0

Differentiate between the call by reference and call by value

I'm learning c++. And I need a answer immediately. Please help me out.

14th Mar 2018, 7:30 AM
Mayank Karemore
1 Réponse
+ 11
Passing a variable by value, is what it is - You pass the value of the variable into the function, not the actual variable itself. Hence, any changes made to the parameter does not affect the original variable which was passed into the function. void test(int num) { num = 10; } int main() { int value = 5; test(value); std::cout << value; // prints 5 } Passing a variable by reference to a function means that you pass the variable to the function as a reference of the original variable. Any changes done to the reference variable is reflected by the original variable. void test(int& num) { num = 10; } int main() { int value = 5; test(value); std::cout << value; // prints 10 }
14th Mar 2018, 7:36 AM
Hatsy Rei
Hatsy Rei - avatar