Questions tagged [pass-by-value]

pass-by-value is a "one way passing" so that modifications of the passed value inside the receiving function (or other entity like process, etc) are not returned back.

pass-by-value is a one way passing so that modifications of the passed value inside the receiving function (or other entity like process, etc) are not returned back.

pass-by-value normally means cloning a variable and making a cloned copy available to the receiving function. Modifications of this copy are allowed, but they are not returned back to the caller; the copy is simply disposed after the call returns.

Passing by value is more secure if the function can also be called by malicious code (malicious code can retain the reference and observe it later even if it cannot modify it). Some are also convinced it results a cleaner code. However passing of the larger structures by value also requires more resources.

1173 questions
33
votes
6 answers

Const correctness for value parameters

I know there are few question about const correctness where it is stated that the declaration of a function and its definition do not need to agree for value parameters. This is because the constness of a value parameter only matters inside the…
jmucchiello
  • 18,754
  • 7
  • 41
  • 61
31
votes
3 answers

returning value vs pointer in Go constructor

When building a simple object in go, what's the difference between these alternatives? func NewGender(value string) Gender { return Gender{strings.TrimSpace(value)} } func NewGender(value string) *Gender { return…
Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
30
votes
1 answer

Best form for constructors? Pass by value or reference?

I'm wondering the best form for my constructors. Here is some sample code: class Y { ... } class X { public: X(const Y& y) : m_y(y) {} // (a) X(Y y) : m_y(y) {} // (b) X(Y&& y) : m_y(std::forward(y)) {} // (c) Y m_y; } Y f() { return…
Clinton
  • 22,361
  • 15
  • 67
  • 163
26
votes
7 answers

Does C# pass a List to a method by reference or as a copy?

Taking my first steps in C# world from C/C++, so a bit hazy in details. Classes, as far as I understood, are passed by reference by default, but what about eg. List like in: void DoStuff(List strs) { //do stuff with the list of…
Scre
  • 454
  • 1
  • 10
  • 15
25
votes
4 answers

Copy elision for pass-by-value arguments

Given struct Range{ Range(double from, double to) : from(from), to(to) {} double from; double to; }; struct Box{ Box(Range x, Range y) : x(x), y(y) {} Range x; Range y; }; suppose we run Box…
Museful
  • 6,711
  • 5
  • 42
  • 68
24
votes
11 answers

Confused, whether java uses call by value or call by reference when an object reference is passed?

public class program1{ public static void main(String args[]){ java.util.Vector vc=new java.util.Vector(); vc.add("111"); vc.add("222"); functioncall(vc); vc.add("333"); …
user617597
  • 788
  • 3
  • 9
  • 21
23
votes
4 answers

Why don't the absolute value functions in C accept const inputs?

In C, the prototype for the absolute value function (that accepts a float) is float fabsf( float ); Why doesn't this prototype accept a constant value, like this: float fabsf( float const ); fabsf won't change the value of the input, will it? If…
user24205
  • 481
  • 5
  • 15
23
votes
9 answers

C# pass by value vs. pass by reference

Consider the following code (I have purposefully written MyPoint to be a reference type for this example) public class MyPoint { public int x; public int y; } It is universally acknowledged (in C# at least) that when you pass by reference,…
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
23
votes
4 answers

How to pass a variable by value to an anonymous javascript function?

The Objective I want to dynamically assign event handlers to some divs on pages throughout a site. My Method Im using jQuery to bind anonymous functions as handlers for selected div events. The Problem The code iterates an array of div names and…
rism
  • 11,932
  • 16
  • 76
  • 116
22
votes
3 answers
22
votes
3 answers

How are arrays passed?

Are arrays passed by default by ref or value? Thanks.
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
22
votes
3 answers

why function objects should be pass-by-value

I have just read the classic book "Effective C++, 3rd Edition", and in item 20 the author concludes that built-in types, STL iterators and function object types are more appropriate for pass-by-value. I could well understand the reason for built-in…
JavaBeta
  • 510
  • 7
  • 16
21
votes
1 answer

Dynamic memory access only works inside function

This question is meant to be used as a canonical duplicate for this FAQ: I am allocating data dynamically inside a function and everything works well, but only inside the function where the allocation takes place. When I attempt to use the same data…
Lundin
  • 195,001
  • 40
  • 254
  • 396
20
votes
4 answers

Are swift classes inside struct passed by copy during assignment?

If I have a struct in swift with inside a class attribute and I copy the struct object, is the class attribute copied or passed by reference?
Nisba
  • 3,210
  • 2
  • 27
  • 46
19
votes
3 answers

Why is the copy constructor called when we pass an object as an argument by value to a method?

Why is the copy constructor called when I pass an object as an argument by value to a function? Please see my below code: I am passing an object of a class as an argument by value to a function display(), but it is calling the copy constructor…
nagaradderKantesh
  • 1,672
  • 4
  • 18
  • 30