[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5711
  • Last Modified:

Call by address Vs Call by Reference

What is the difference between call by address and call by reference?

thanks
0
arijit_rebaca
Asked:
arijit_rebaca
1 Solution
 
e_tadeuCommented:
Functionally they are the same thing, only differing in syntax.
Call-by-address is an "inheritance" from the C language, where references did not exits. You simply pass a pointer to a variable (it's memory address):

void call_by_address(int *x) {
  *x = (*x) + 5;
}

int main() {
  int n;
  n = 10;
  call_by_address(&n);

  cout << n; // should print 15
 
  return 0;  
}

With Call-by-reference (which only exists in C++), you can "implicitly" pass it's memory address and still call the function as you would call in a call-by-value, and you do not need to explicitly get the variable address (&n) and to dereference the pointer (*x), you use the parameter as a normal parameter:

void call_by_reference(int &x) {
  x = x + 5;
}

int main() {
  int n;
  n = 10;
  call_by_reference(n);

  cout << n; // should print 15
 
  return 0;  
}

0
 
arijit_rebacaAuthor Commented:
So there is no difference in between address and reference.. If diffrence exist then explain in detail......

thanks,
0
 
rajeev_devinCommented:
>> So there is no difference in between address and reference.. If diffrence exist then explain in detail......
There are differences actually.
You can have a uninitialized pointer, but you cannot have uninitilized reference.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Pierre CorneliusCommented:
The difference between passing values/parameters to a function is that changes made by a function to the value or parameter are local to the function if you pass it by value and global if you pass it as a reference.

If your function takes a parameter by value, it is like a local variable to that function is created and assigned the value of the parameter, so changes made to it will only be visible to the function.

If your function takes a parameter by reference, then any changes made by the function will affect it within the scope it was called in.

Analogy:
By value: I give you a notepad to use, modify etc. and you throw it away when your done.
By reference: I give you a notepad to use, modify etc. and you return it to me when you are done.


@e_tadeu
>>>With Call-by-reference (which only exists in C++)...
Do you mean only exists in C++ as opposed to C because many other language also supports this functionality (e.g. Delphi)


Regards
Pierre
0
 
seet82Commented:
>> The difference between passing values/parameters to a function is that changes made by a function to the value or parameter are local to the function if you pass it by value and global if you pass it as a reference.

we are talking about reference and address here, not values...

0
 
arijit_rebacaAuthor Commented:
Any others differences are there? is reference is a constant pointer.......

rajeev_devin what is your opinion?

thanks,
 
0
 
seet82Commented:
yes reference is a constant pointer
that having said, it means that the memory pointed to by the reference cannot be changed
however, the value in the memory can be changed

my explanation might be a bit confusing, but i'll illustrate it with a example

class Object {
    int a;
}
Object &ref;
Object *p;
Object norm;
Object norm2;
norm.a=1;
norm2.a=2;
int a;
*p=norm; //p points to norm
a=p->a; //a=1

*p=norm2; //p points to norm2
a=p->a; //a=2

ref=&norm; //norm is reference by ref
a=ref.a; //a=1

ref=&norm2; //compilation error as ref is a reference and thus cannot re-assign to another Object


0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now