You are here:

C++/call by refrence

Advertisement


Question
Hi
i have a question about calling variables in functions

could you plz tell me what's the diffrence between:

power(&a)     and     power(*a)

i know that in power(&a) the address of 'a' will be change but will the value of 'a' change too?
or in power(*a) the value of a will change but will the address of it change too?

i mean the main values and addresses,not the values or addresses that we use in function power

Thanx
Bita


Answer
Hi dear Bita !

Sorry for the delay in responding ! , first of all lemme give you a simple program to understand the concept of pointer and reference!


#include "iostream"

using namespace std;

void ByValue(int x)
{
  x = x+10;
}

void ByReference(int& x) // using reference mechanism
{
  x = x+10;
}

void ByRef(int* x) // using pointer mechanism
{
  *x = *x + 10;
}

void main()
{
  int a = 10;
  int *p = &a;

  ByValue(a); // Function call by value

  cout<<"Value of a after calling ByValue(a) "<<a<<endl; // no change in value of a
  ByReference(a);// Function call by reference
  cout<<"Value of a after calling ByReference(a); "<<a<<endl; // change in value of a

  ByValue(*p); // Function call  by value
  cout<<"Value of a after calling ByValue(*p) "<<a<<endl; // no change in value of a
  
  ByRef(p); // Function call by reference
  cout<<"Value of a after calling ByRef(p); "<<a<<endl; // Change in value of a

  ByRef(&a); // function call by reference
  cout<<"Value of a after calling ByRef (&a); "<<a<<endl; // Change in value of a
}


/*
  O/P

  Value of a after calling ByValue(a) 10
  Value of a after calling ByReference(a); 20
  Value of a after calling ByValue(*p) 20
  Value of a after calling ByRef(p); 30
  Value of a after calling ByRef(&a); 40
  Press any key to continue . . .
*/


  

  Here in this programme , the first function call is self explanatory , since it just uses a functinon call ByValue(a); which is very familiar to all, and the explanation for the same function definition is also simple so lets go to the second !

>>ByReference(a); Here in this case , the address of the variable will be copied into the formal argument x in this function call  void ByReference(int& x)  ie argument x will hold the reference to the actual argument a , what ever changes that has done to the reference variable would affect the actual argument as well , and thats why

void ByReference(int& x) // using reference mechanism
{
  x = x+10;
}

this function call changes the value of actual argument 'a'

int *p = &a;

above initialization statement says that p is a pointer to an integer and it holds the address of a make sure 'p' HOLDES THE ADDRESS OF a ! and now *p just points the value of 'a' the symbol * can be elaborated as   VALUE AT THE ADDRESS OF ie *p can defined as VALUE AT THE ADDRESS OF p
as said earlier p holds the address of 'a'  and hence this expression will return the value of 'a' in this case it would be 20 so the function call ByValue(*p); is quit similar to ByValue(20); which won't affect the actual value of  'a' since because just  the value of 'a' is passed to the function using a pointer

with this call ByRef(p);  you know well 'p' holds the address of the variable 'a'  this function call is to be handles in a different way since it uses a pointer type!  Lets get little bit deeper . The given above function call will correspond to the following function

void ByRef(int* x) // using pointer mechanism
{
  *x = *x + 10;
}

here formal argument 'x' is declared as a pointer and sure it will hold only the address of a variable of type integer . As I said earlier  actual argument 'p' holds the address of variable 'a'  , the same address will be copied to 'x' , the function call ByRef(p); can be defined as ByRef(address of 'a' );
on the other end the 'called ' function will receive

void ByRef(address of 'a')
{
  *x = *x+10;
  
  /*
     Above statement can be defined as below , by expanding the meaning of *
     VALUE AT THE ADDRESS OF 'a' = VALUE AT THE ADDRESS OF 'a'  + 10;
  */
}

ByRef(&a);  this call is similar to the previous one , here we straight away passing the address of 'a' using the & operator while the previous uses the pointer which holds the address of 'a' any how both are functionally same

Only thing you have to keep in mind is WHAT  you are sending to a function ie either value ( value of a variable ) or reference ( address or reference of a variable ).

if its a value type function call then the changes will not reflect on the actual variable , else it will in case of Reference type function call

Note: go through the answer slowly and carefully , sure it will take time to digest, if you couldn't get back to me for more support !


Thanks and Regards !
Prince M., Premnath  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise

Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)

Experience

More than 5 years

Education/Credentials
MCA

©2016 About.com. All rights reserved.