Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Argument Passing "passed by reference"

Tags:

I understand as with any other variable, the type of a parameter determines the interaction between the parameter and its argument. My question is that what is the reasoning behind why you would reference a parameter vs why you wouldn't? Why are some functions parameters reference and some are not? Having trouble understanding the advantages of doing so, could someone explain?

like image 567
Tyler Avatar asked Nov 07 '13 02:11

Tyler


People also ask

Can you pass arguments by reference in C?

To pass a value by reference, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to, by their arguments.

What is passing argument by reference?

Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the argument by using its reference passed in.

How are arguments passed by value or by reference?

When you pass an argument by reference, you pass a pointer to the value in memory. The function operates on the argument. When a function changes the value of an argument passed by reference, the original value changes. When you pass an argument by value, you pass a copy of the value in memory.


2 Answers

The ability to pass by reference exists for two reasons:

  1. To modify the value of the function arguments
  2. To avoid make copies of an object for performance reasons

Example for modifying the argument

void get5and6(int *f, int *s)  // using pointers {     *f = 5;     *s = 6; } 

this can be used as:

int f = 0, s = 0; get5and6(&f,&s);     // f & s will now be 5 & 6 

OR

void get5and6(int &f, int &s)  // using references {     f = 5;     s = 6; } 

this can be used as:

int f = 0, s = 0; get5and6(f,s);     // f & s will now be 5 & 6 

When we pass by reference, we pass the address of the variable. Passing by reference is similar to passing a pointer - only the address is passed in both cases.

For eg:

void SaveGame(GameState& gameState) {     gameState.update();     gameState.saveToFile("save.sav"); }  GameState gs; SaveGame(gs) 

OR

void SaveGame(GameState* gameState) {     gameState->update();     gameState->saveToFile("save.sav"); }  GameState gs; SaveGame(&gs); 


Since only the address is being passed, the value of the variable (which could be really huge for huge objects) doesn't need to be copied. So passing by reference improves performance especially when:
  1. The object passed to the function is huge (I would use the pointer variant here so that the caller knows the function might modify the value of the variable)
  2. The function could be called many times (eg. in a loop)

Also, read on const references. When it's used, the argument cannot be modified in the function.

like image 170
galdin Avatar answered Jan 08 '23 22:01

galdin


This article helped me a lot.

Please forget about pointers for now. And take this with a grain of salt.

A reference is the object. When you pass by reference, you pass the object.

When you pass by value, you pass a copy of the object; another object. It may have the same state, but it is a different instance; a clone.

So, it may make sense to pass by reference if you:

  • need to modify the object inside the function
  • do not need (or want) to modify the object, but would like to avoid copying it just to pass it to a function. This would be a const reference.

And it may make sense to pass by value if you:

  • want to start from an identical twin, and leave the original twin undisturbed
  • do not care about the cost of copying the object (for example, I would not pass an int by reference unless I wanted to modify it).

Here, have a look at this code:

#include<iostream>  struct Foo {   Foo() { }   void describe() const {     std::cout<<"Foo at address "<<this<<std::endl;   }   };  void byvalue(Foo foo) {   std::cout<<"called byvalue"<<std::endl;   foo.describe(); }  void byreference(Foo& foo) {   std::cout<<"called byreference"<<std::endl;     foo.describe(); }  int main() {   Foo foo;   std::cout<<"Original Foo"<<std::endl;   foo.describe();   byreference(foo);   byvalue(foo); } 

And compile it like this: g++ example.cpp

Run it: ./a.out

And check the output (the actual addresses may be different in your computer, but the point will remain):

Original Foo Foo at address 0x7fff65f77a0f called byreference Foo at address 0x7fff65f77a0f called byvalue Foo at address 0x7fff65f779f0 

Notice how the called byreference address is the same as the Original Foo address (both are 0x7fff65f77a0f). And notice how the called byvalue address is different (it is 0x7fff65f779f0).

Take it up a notch. Modify the code to look as follows:

#include<iostream> #include<unistd.h> // for sleeping  struct Foo {   Foo() { }   Foo(const Foo&) {     sleep(10); // assume that building from a copy takes TEN seconds!   }   void describe() const {     std::cout<<"Foo at address "<<this<<std::endl;   }   };  void byvalue(Foo foo) {   std::cout<<"called byvalue"<<std::endl;   foo.describe(); }  void byreference(Foo& foo) {   std::cout<<"called byreference"<<std::endl;     foo.describe(); }  int main() {   Foo foo;   std::cout<<"Original Foo"<<std::endl;   foo.describe();   byreference(foo);   byvalue(foo);   } 

Compile it the same way, and notice the output (comments not in the output; included for clarity):

Original Foo Foo at address 0x7fff64d64a0e called byreference Foo at address 0x7fff64d64a0e # this point is reached "immediately" called byvalue # this point is reached TEN SECONDS later Foo at address 0x7fff64d64a0f 

So, the code is meant to exaggerate the cost of a copy: when you called by reference this cost was NOT incurred. When you called by value you had to wait for ten seconds.

Note: my code was compiled in OS X 10.7.4 using GCC 4.8.1. If you are in windows you may need something different from unitsd.h to make the sleep call work.

Maybe this helps.

like image 33
Escualo Avatar answered Jan 08 '23 21:01

Escualo