hello , I want to know what is the difference between passing by value and reference , and if you can put an example with simple out put .

Recommended Answers

All 10 Replies

Passing by value is better described as pass by copy. A copy of the value is made and sent to a function. This means that if you change the value in the function, the original remains the same:

#include <iostream>

void foo ( int copy )
{
  std::cout<<"In foo the copy is "<< copy <<'\n';
  copy = 54321;
  std::cout<<"Changing the copy to "<< copy <<'\n';
}

int main()
{
  int original = 12345;

  std::cout<<"In main the value is "<< original <<'\n';
  foo ( original );
  std::cout<<"Back in main the value is "<< original <<'\n';
}

Passing by reference means that instead of a copy of the value, you pass the actual object that contains the value. That way when you change the value, the change is also made in the original object:

#include <iostream>

void foo ( int& reference )
{
  std::cout<<"In foo the reference is "<< reference <<'\n';
  reference = 54321;
  std::cout<<"Changing the reference to "<< reference <<'\n';
}

int main()
{
  int original = 12345;

  std::cout<<"In main the value is "<< original <<'\n';
  foo ( original );
  std::cout<<"Back in main the value is "<< original <<'\n';
}
void foo( int val ) {
  val = 1;
}
void foo2( int &ref ) {
  ref = 1;
}

First is by value, second is by reference. You can change the value of 'ref' in foo2 by setting its value and the value will change outside the scope of the function, but in foo the value only changes within foo's scope. Not outside ...

// with regard to the above two functions
int main( void ) {
  int num = 2;

  foo( num ); std::cout << num << "\n";
  foo2(num ); std::cout<< num << "\n";

  return 0;
}

The output should be
2
1

that is mean in passing by reference the changing in the int main , also affect the function .

or I think the opposite is right .

Sorry, I can't parse what you're trying to say.

He's still not clear which is which.

bis student
At this point, you should copy Narue's code into a file, compile it, and run it. Do it for both programs. That's how you will see what does what.

pass by value...
it just send the value of the variable(one memory location) to the function...and that value is stored in another variable(another memory location) that is declared in the function... void foo(int b); when you call this function from the main like.. foo(a); the value stored in memory allocated to integer a is copied to the memory allocated to the integer b in the function foo...if u make any change in value of b inside the function foo ; it will not affect the value in memory allocated to variable a in the main program.....because the two have physically different memory allocated to them

pass by reference...
this is opposite to pass by value...here you send the actual memory location address and the change is made in this ...
example void foo(int &c) if you call this function from main like... foo(a); the address of the memory location allocated to a will be sent to c declared in function foo... and if you make any change in this memory location inside the function foo the change will be made in the value of a also...because the original memory location has been modified...because c is having the memory address of a ...

i hope you got it now...

underneath, passing by reference involves passing the pointer to the thing being referenced; maybe this will give some insights into how it works

so in the example from above,

void foo( int val ) {
  val = 1;
}
void foo2( int &ref ) {
  ref = 1;
}
int main( void ) {
  int num = 2;

  foo( num ); std::cout << num << "\n";
  foo2(num ); std::cout<< num << "\n";

  return 0;
}

is equivalent to this in C:

void foo( int val ) {
  val = 1;
}
void foo2( int *ref ) {
  *ref = 1;
}
int main( void ) {
  int num = 2;

  foo( num ); printf("%d\n", num);
  foo2(&num ); printf("%d\n", num);

  return 0;
}

notice how the reference declaration is equivalent to a pointer declaration, except that every time you use the reference, it is dereferenced automatically without you having to use * explicitly like with a pointer; and whenever you call a method with a reference argument, or assign to a reference, you take the address of the variable, without explicitly using & like with a pointer

also note that in the C++ code above, this won't work:

foo2(5)

because the reference needs to have an addressable expression to point to

Hi i have done intruduction in c++ and would like to know how many libraries does it have

Many hundreds if not thousands, probably.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.