Questions tagged [pass-by-reference]

Pass by reference is an argument marshalling strategy whereby a variable's location in memory is passed to a function, rather than a copy of the variable's value, although the function appears in the source code to receive the variable itself rather than a pointer to it.

Passing by reference means that the memory address of a variable is passed rather than a copy of the variable's value.

This typically means that the function can modify the passed variable, assigning a new value to it. However for performance reasons, passing by reference may be useful even if the passed structure is not modified, as with the Pascal var modifier, and some programming languages have constructs (like the C const modifier) to disallow modification of a variable passed by reference.

4485 questions
10
votes
11 answers

pass by reference c++

My teacher in c++ told me that call by reference should only be used if I'm not going to change anything on the arrays inside the function. I have some really big vectors that I'm passing around in my program. All the vectors will be modified inside…
user119653
10
votes
2 answers

Is Objective-C pass-by-value or pass-by-reference?

Since we always use pointers to define variables, I was wondering if Objective-C is "pass by value", since like Java, the actual value would be passed by using its reference. However, since it seems to be built up on top of C, would it have all the…
gran_profaci
  • 8,087
  • 15
  • 66
  • 99
10
votes
4 answers

Pass by reference more expensive than pass by value

Is there a case where pass-by-reference is more expensive than pass-by-value in C++? If so, what would that case be?
erelender
  • 6,175
  • 32
  • 49
10
votes
7 answers

Any differences between f(const string &) and f(const string )?

class mystring { friend ostream& operator<<(ostream &out, const mystring ss) { out << ss.s; return out; } private: string s; public: mystring(const char ss[]) { cout << "constructing mystring : " << ss << endl; …
Jichao
  • 40,341
  • 47
  • 125
  • 198
10
votes
4 answers

Reference found where even-sized list expected in Perl - Possible pass-by-reference error?

I have a Perl class/module that I created to display Bible verses. In it there is a hash that stores several verses, with the key being the book/chapter/verse and the value being the text. This hash is returned from the module. I'm including the…
10
votes
2 answers

Why can't I use static_cast to pass an integer reference parameter to a function in C++?

I have an enum parameter in a C++ program that I need to obtain using a function that returns the value through a parameter. I started by declaring it as an int but at code review was asked to type it as the enum (ControlSource). I did this but it…
Gareth
  • 439
  • 5
  • 12
10
votes
4 answers

prevent pass-by-ref of temporary object

I have a class that 'remembers' a reference to some object (e.g. an integer variable). I can't have it reference a value that's destructed immediately, and I'm looking for a way to protect the users of my class from doing so by accident. Is an…
xtofl
  • 40,723
  • 12
  • 105
  • 192
9
votes
2 answers

Are Matlab Matrices transferred pass-by-value or pass-by-reference?

I'm new at Matlab. You may find this question silly but I really wonder if the statement below is a pass-by-value operation or pass-by-reference operation. I = imread('logo.png'); binaryImage = im2bw(I, 0.4); Itemp = binaryImage; Does the Itemp is…
Yunus Eren Güzel
  • 3,018
  • 11
  • 36
  • 63
9
votes
1 answer

Pass by reference or return array in PHP?

All of my functions that have multiple parameters and that need to return more than one of those values I return an array like so... function eg($a, $b) { $a += 5; $b += 10; return array('a' => $a, 'b' => $b); } $no = eg(0, 5); echo…
cgwebprojects
  • 3,382
  • 6
  • 27
  • 40
9
votes
4 answers

Does passing Reference Types using ref save memory?

In C#, the parameters to a method can be either reference types or value types. When passing reference types, a copy of the reference is passed. This way, if inside a method we try to reassign the passed reference to another object instance, outside…
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
9
votes
1 answer

PHP foreach statement by reference: unexpected behaviour when reusing iterator

this code produce an unexpected output: $array=str_split("abcde"); foreach($array as &$item) echo $item; echo "\n"; foreach($array as $item) echo $item; output: abcde abcdd if use &$item for second loop everything works fine. I don't…
Frederic Bazin
  • 1,530
  • 12
  • 27
9
votes
2 answers

Trying to use templatised fuctions to swap two strings

#include #include template void swap(T a , T b) { T temp = a; a = b; b = temp; } template void swap1(T1 a , T1 b) { T1 temp = a; a = b; b = temp; } int main() { int a = 10 , b = 20; …
9
votes
4 answers

__callStatic(), call_user_func_array(), references, and PHP 5.3.1

I've been reading around on SO, and elsewhere, however I can't seem to find anything conclusive. Is there any way to effectively carry references through this call stack, resulting in the desired functionality as described in the example below?…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
9
votes
2 answers

Is There a Reason Standard Algorithms Take Lambdas by Value?

So I asked a question here: Lambda Works on Latest Visual Studio, but Doesn't Work Elsewhere to which I got the response, that my code was implementation defined since the standard's 25.1 [algorithms.general] 10 says: Unless otherwise specified,…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
9
votes
4 answers

Javascript: Re-assigning a function with another function

Let's say I have these two functions: function fnChanger(fn) { fn = function() { sys.print('Changed!'); } } function foo() { sys.print('Unchanged'); } Now, if I call foo(), I see Unchanged, as expected. However, if I call fnChanger first, I…
Austin Hyde
  • 26,347
  • 28
  • 96
  • 129