+ 3

Difference between call by reference and call by address?

26th Apr 2017, 5:34 AM
Posi2
Posi2 - avatar
7 Antworten
26th Apr 2017, 6:06 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 5
do you mean "pass by reference" and "pass by value"? You can try to understand with this link: https://en.wikipedia.org/wiki/Function_prototype
26th Apr 2017, 5:51 AM
Nithiwat
Nithiwat - avatar
+ 5
if I make program (the code below) #include <iostream> using namespace std; void set(int x) { x = 1; } int main() { int n = 5; set(n); cout<<n; return 0; } this code will output 5 **It's call pass by value** this function doesn't reference the address of variable n.
26th Apr 2017, 6:01 AM
Nithiwat
Nithiwat - avatar
+ 4
Then I create program: #include <iostream> using namespace std; void set(int &x) { x = 1; } int main() { int n = 5; set(n); cout<<n; return 0; } Lets see this code ('&' is added in set function) It will make output 1 because it reference the address of integer n. And it will edit the value of variable n directly.
26th Apr 2017, 6:09 AM
Nithiwat
Nithiwat - avatar
+ 4
you can imagine : my c++ programs run in rams memory Then I run program value of variable n in 2nd code is already in the ram... Of course! integer n had the address to call it (edit value,read value) '&' sign is refer to address of variable x (parameters) Don't forget '''x = n''' because ''n'' is argument of ''x''.
26th Apr 2017, 6:55 AM
Nithiwat
Nithiwat - avatar
+ 3
Pass by reference: use the memory address of an object in order to compute on it directly. Every calculation directly affects the object. Pass by value: create a copy of an object to be used in your calcualtions. Original object is not affected by the calculations done on its copy.
26th Apr 2017, 6:32 AM
seamiki
seamiki - avatar
+ 1
@nithiwat can you explain second code ? how it is work on memory? we are not passing address how it works? or access Another local variable
26th Apr 2017, 6:16 AM
Posi2
Posi2 - avatar