0

What is the purpose of having parameters as call-by-value when it is more efficient to use call-by-reference? (For non-primitive data types.)

Also, what if one was to add the const tag to call-by-reference parameters so that they won't be modified?

Is there a situation where its beneficial to use call-by-value as opposed to call-by-reference?

  • 1
    Your premise is flawed. Passing by reference isn't necessarily more efficient, especially for simple types such as int and bool. Also, with reference parameters, you risk side effects of having the value of a variable changed. – Dan Korn Feb 09 '15 at 17:47
  • @RedX Is there a version of that question that's not already closed? – Mark B Feb 09 '15 at 17:47

7 Answers7

1

Excerpt I have saved that you may find useful (from CodingUnitTutorials )

There are two possible reasons to not use call-by-reference: side effects and privacy. Unwanted side effects are usually caused by inadvertently changes that are made to a call by reference parameter. Also in most cases you want the data to be private and that someone calling a function only be able to change if you want it. So it is better to use a call by value by default and only use call by reference if data changes are expected.

HavelTheGreat
  • 3,299
  • 2
  • 15
  • 34
  • What about adding const to a call-by-reference parameter as opposed to the default call-by-value? –  Feb 09 '15 at 17:49
1

In any case where for whatever reason your function body already needs a copy of the parameter, it's typically more efficient to pass that parameter by value, because in some cases that copy can actually be eliminated by the compiler.

Mark B
  • 95,107
  • 10
  • 109
  • 188
1

It is not the case that call-by-reference is always more efficient than call-by-value. Passing an integer rather than a reference to an integer is usually one indirection cheaper. Depending on the parameter list, some values could be passed in registers rather than indirectly having to dereferenced in memory somewhere

Shripathi Kamath
  • 310
  • 3
  • 10
1

it is more efficient to use call-by-reference?

This is far from being a certainty: there are situations when passing parameters by value is more efficient than passing parameters by reference, because you pass "less than a pointer worth" of data.

Is there a situation where its beneficial to use call-by-value as opposed to call-by-reference?

Passing by value implies copying. You use it when you would like to ensure that you get a copy of an object, rather than the object itself. For example, if you would like to ensure that the object is not being modified concurrently with your code using it, write a function that receives its parameters by value.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

There are cases where pass by value is simply what you want, regardless of efficiency--if you want to be able to pass something and manipulate/modify it without affecting the original, then pass by value may be exactly the right thing, and if it's slower then it's slower.

For one real example, consider that we're dealing with a priority_queue--but under some circumstances, we want to write the current contents of that priority queue to a log (in order). Reading from a priority queue is destructive, but we need the original intact. One obvious solution: pass the priority queue to the logging function by value. The logging function does its destructive reads from the queue, but when it's done, the surrounding code still has the original queue, un-modified.

There are also a fair number of cases where passing by value is actually more efficient than passing by reference.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

Most often this call by value is used when the original data shouldn't be changed and a duplicate of the reference would be created inside the function anyway.

Nielsvh
  • 1,151
  • 1
  • 18
  • 31
0

One example is where the variable is volatile, meaning it can be changed by another thread of execution or by something outside the program, like hardware registers.

When passing by value, a copy or snapshot of the variable is taken before passing to the function. When passing by reference, the functional code will get the value at the time the variable is accessed. This could lead to different values depending on if and when the volatile variable is changed.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154