Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is the difference between call by reference and call by address? can we say the following two programs represents the call by reference concept?

Program-1..........................................................

C++
int main()
{
    void fun(int *x);
    
    int a=10;
    cout<<a<<endl;
    
    fun(&a);
    
    cout<<a;
    getch();
    return 0;
}

void fun(int *x)
{
     (*x)++;
}




Program-2........................................................................

C++
int main()
{
    void fun(int& x);
    
    int a=10;
    cout<<a<<endl;
    
    fun(a);
    
    cout<<a;
    getch();
    return 0;
}

void fun(int& x)
{
     x++;
}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 26-Feb-12 1:33am
v2

In practicle terms, there is no real difference - call by reference is a syntactic sugar to prevent you having to pass a pointer.
Yes, you second example does show call by reference, but it might be easier to look at it if you did this:

C++
void fun(int x, int& y, int* z);
int main()
   {
   int a = 10;
   int b = 20;
   int c = 30;
   fun(a, b, &c);
   cout << a << b << c;	
   return 0;
   }
void fun(int x, int& y, int* z)
   {
   x++;
   y++;
   *z++;
   }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 27-Feb-12 0:45am    
Good, my 5.
I added a couple of important notes in my answer, please see.
--SA
Bhavin Jagad 28-Feb-12 5:06am    
whenever you don't want to seek pointer position inside called function, call by reference is preferable or pass the pointer as "const datatype* ".
Bjarne Stroustrup explains why he introduced references -
Why does C++ have both pointers and references?[^]
 
Share this answer
 
An answer by Griff is correct, but I want to clarify couple of things.

First, in both cases, the binary layout of the code and stack is exactly the same.

Secondly, this syntactic sugar is important enough during compile time. One very important aspect: passing by reference is the way to prevent passing a null pointer, because you cannot pass null reference. It works like a syntactically-enforced precondition of the call.

—SA
 
Share this answer
 
Comments
Emilio Garavaglia 5-Mar-12 12:26pm    
" because you cannot pass null reference."
According with the standard yes, this is "undefined behavior".
And since C++11 the stabdard says "it is an error to take the address of areference".

Butbefore that compilers allow do the trick:
<pre>
void fn(A& a)
{
if(!&a) cout << "null reference" <<endl;
else cout << a << endl; //admitting << works for A
}
int main()
{
A an_a;
fn(an_a);
fn(*(A*)0);
}
</pre>

This is not orthodox, but the concept of "null reference" could be defined.
Today anymore.
Sergey Alexandrovich Kryukov 5-Mar-12 13:10pm    
Emilio,

Thank for your interesting note.

I mean something more simple:
Having

void MyMethod(int & myParameter)

by syntax, it's not possible to pass
MyMethod(null)

but in equivalent case
void MyMethod(int * myParameter)

one can do both
int myActialParameter = //...
MyMethod(&myActualParameter)
or
MyMethod(null).

This way, an equivalent variant with reference presents limitation, which can be very useful, because you don't have to consider a precondition "!= null" if it is required.

I think you would agree this is an important and useful feature of pass-by-reference.

--SA
Emilio Garavaglia 5-Mar-12 14:34pm    
"I think you would agree..." Of course.

Unless you need to give citizenship to the concept of "not existent".
In that case, a pointer is useful.
And where reference where required ... I saw sometimes the *(A*)0 trick!
Sergey Alexandrovich Kryukov 5-Mar-12 15:42pm    
Absolutely. I often use null as a concept.

There is a big paradigm of never using nulls, which I don't really like. There is also a very different thing, "null object" design pattern, which also tries to avoid null, introducing "special object" instead of null.

--SA
when you call by address and pass you variable into a function or anywhere, function will create its copy and work with it without changing real variable. so function will do its work and leave your variable untouched.
if you pass variable into function and you need to change its value call by reference, thus you will work with what is located under the address of the variable
when you pass it by reference, the function gets the real value, I mean it works with what is stored under the address of variable and changes it.
 
Share this answer
 
Comments
C.O.W. 10-Sep-13 0:39am    
Among the two programs given above , Program-1 is an example of Call By Address where Program-2 is an example of Call By Reference .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900