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
145
votes
7 answers

python pandas dataframe, is it pass-by-value or pass-by-reference

If I pass a dataframe to a function and modify it inside the function, is it pass-by-value or pass-by-reference? I run the following code a = pd.DataFrame({'a':[1,2], 'b':[3,4]}) def letgo(df): df = df.drop('b',axis=1) letgo(a) the value of a…
nos
  • 19,875
  • 27
  • 98
  • 134
137
votes
13 answers

Passing an integer by reference in Python

How can I pass an integer by reference in Python? I want to modify the value of a variable that I am passing to the function. I have read that everything in Python is pass by value, but there has to be an easy trick. For example, in Java you could…
CodeKingPlusPlus
  • 15,383
  • 51
  • 135
  • 216
136
votes
10 answers

Is Swift Pass By Value or Pass By Reference

I'm really new to Swift and I just read that classes are passed by reference and arrays/strings etc. are copied. Is the pass by reference the same way as in Objective-C or Java wherein you actually pass "a" reference or is it proper pass by…
gran_profaci
  • 8,087
  • 15
  • 66
  • 99
127
votes
5 answers

Are slices passed by value?

In Go, I am trying to make a scramble slice function for my traveling salesman problem. While doing this I noticed when I started editing the slice I gave the scramble function was different every time I passed it in. After some debugging I found…
duck
  • 1,674
  • 2
  • 16
  • 26
119
votes
6 answers

How do I pass the value (not the reference) of a JS variable to a function?

Here is a simplified version of something I'm trying to run: for (var i = 0; i < results.length; i++) { marker = results[i]; google.maps.event.addListener(marker, 'click', function() { change_selection(i); }); } but I'm…
ryan
  • 2,311
  • 3
  • 22
  • 28
116
votes
17 answers

C++ - passing references to std::shared_ptr or boost::shared_ptr

If I have a function that needs to work with a shared_ptr, wouldn't it be more efficient to pass it a reference to it (so to avoid copying the shared_ptr object)? What are the possible bad side effects? I envision two possible cases: 1) inside the…
abigagli
  • 2,769
  • 4
  • 29
  • 32
95
votes
2 answers

Isn't "const" redundant when passing by value?

I was reading my C++ book (Deitel) when I came across a function to calculate the volume of a cube. The code is the following: double cube (const double side){ return side * side * side; } The explanation for using the "const" qualifier was…
Daniel Scocco
  • 7,036
  • 13
  • 51
  • 78
85
votes
5 answers

Does C++ pass objects by value or reference?

A simple question for which I couldn't find the answer here. What I understand is that while passing an argument to a function during call, e.g. void myFunction(type myVariable) { } void main() { myFunction(myVariable); } For simple datatypes…
user3041058
  • 1,520
  • 2
  • 12
  • 16
85
votes
2 answers

What exactly is copy-on-modify semantics in R, and where is the canonical source?

Every once in a while I come across the notion that R has copy-on-modify semantics, for example in Hadley's devtools wiki. Most R objects have copy-on-modify semantics, so modifying a function argument does not change the original value I can…
Andrie
  • 176,377
  • 47
  • 447
  • 496
84
votes
7 answers

Is the pass-by-value-and-then-move construct a bad idiom?

Since we have move semantics in C++, nowadays it is usual to do void set_a(A a) { _a = std::move(a); } The reasoning is that if a is an rvalue, the copy will be elided and there will be just one move. But what happens if a is an lvalue? It seems…
jbgs
  • 2,795
  • 2
  • 21
  • 28
81
votes
8 answers

C++ view types: pass by const& or by value?

This came up in a code review discussion recently, but without a satisfactory conclusion. The types in question are analogues to the C++ string_view TS. They are simple non-owning wrappers around a pointer and a length, decorated with some custom…
acm
  • 12,183
  • 5
  • 39
  • 68
80
votes
8 answers

Passing values in Python

When you pass a collection like list, array to another function in python, does it make a copy of it, or is it just a pointer?
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
77
votes
8 answers

Emulating pass-by-value behaviour in python

I would like to emulate the pass-by-value behaviour in python. In other words, I would like to make absolutely sure that the function I write do not modify user supplied data. One possible way is to use deep copy: from copy import deepcopy def…
Boris Gorelik
  • 29,945
  • 39
  • 128
  • 170
74
votes
7 answers

How do I modify a pointer that has been passed into a function in C?

So, I have some code, kind of like the following, to add a struct to a list of structs: void barPush(BarList * list,Bar * bar) { // if there is no move to add, then we are done if (bar == NULL) return;//EMPTY_LIST; // allocate space for…
Paul Wicks
  • 62,960
  • 55
  • 119
  • 146
58
votes
5 answers

Array and slice data types

I found myself confused with the array and slice data types. From Go docs, arrays are described as follows: There are major differences between the ways arrays work in Go and C. In Go, Arrays are values. Assigning one array to another copies all…
duganets
  • 1,853
  • 5
  • 20
  • 31
1
2
3
78 79