+ 3
Difference between call by reference and call by address?
7 odpowiedzi
+ 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
+ 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.
+ 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.
+ 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''.
+ 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.
+ 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