0
call by values and call by reference functions..?????
3 Réponses
+ 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.
+ 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 = #
int D(int* byReference){
return ++*byReference;
}
D(ptr);
cout << num; // 4
0
Yeah