pass-by-reference

Is there a rational explanation for this PHP call-by-value behavior? Or PHP bug?

对着背影说爱祢 提交于 2019-12-22 02:05:23
问题 PHP 5.5.12. Consider this: <?php $a = [ 'a', 'b', 'c' ]; foreach($a as &$x) { $x .= 'q'; } print_r($a); This, as expected, outputs: Array ( [0] => aq [1] => bq [2] => cq ) Now consider: <?php $a = [ 'a', 'b', 'c' ]; foreach(z($a) as &$x) { $x .= 'q'; } print_r($a); function z($a) { return $a; } This outputs: Array ( [0] => aq [1] => bq [2] => cq ) (!) But wait a minute. $a is not being passed by reference. Which means I should be getting a copy back from z(), which would be modified, and $a

PHP foreach statement by reference: unexpected behaviour when reusing iterator

旧街凉风 提交于 2019-12-21 09:16:48
问题 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 understand how this code would affect the content of $array . I could consider that an implicit unset($header) would delete the last row but where does the double dd comes from ? 回答1: This could help: $array=str_split("abcde"); foreach($array as &$item) echo

Passing a parameter versus returning it from function

China☆狼群 提交于 2019-12-21 04:45:37
问题 As it might be clear from the title which approach should we prefer? Intention is to pass a few method parameters and get something as output. We can pass another parameter and method will update it and method need not to return anything now, method will just update output variable and it will be reflected to the caller. I am just trying to frame the question through this example. List<String> result = new ArrayList<String>(); for (int i = 0; i < SOME_NUMBER_N; i++) { fun(SOME_COLLECTION.get

Should small simple structs be passed by const reference?

ぃ、小莉子 提交于 2019-12-20 17:33:44
问题 I have always been taught that non-primitive types should be passed by const reference rather than by value where possible, ie: void foo(std::string str);//bad void foo(const std::string &str);//good But I was thinking today that maybe actually some simple user defined types may actually be better passed by value eg: class Vector2 { public: float x, y; ...constructors, operators overloads, utility methods, etc... }; void foo(Vector2 pos); void foo(const Vector2 &pos);//is this really better

Java pass by reference issue [duplicate]

五迷三道 提交于 2019-12-20 04:58:20
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Is Java pass by reference? I have this class here: public class Cat { private String catNum; private static Cat cat1; private static Cat cat2; public Cat(String catNumber) { catNum = catNumber; } public static void change(Cat cat1, Cat cat2) { Cat temp = cat1; cat1 = cat2; cat2 = temp; } public String toString() { return "cat number: " + catNum; } public static void main(String[] args) { cat1 = new Cat("1");

javascript pass by reference workaround

那年仲夏 提交于 2019-12-20 03:32:27
问题 let's say I've got an array, var animals = ["dog","cat","rat"]; then I define var pets = animals; then I call pets.shift(); now because javascript is pass-by-reference for arrays, if I now call animals , I get ["cat","rat"] . My question: is there any way around this, if I would later like to use animals in its unmodified form? 回答1: A note on terminology: JavaScript is never pass-by-reference (no matter how many times you hear people say that it is). When you write the line: var pets =

Pass by Value and Pass by Reference

萝らか妹 提交于 2019-12-20 03:12:33
问题 I'm working on a project that calculate income for employees by using overloading and also pass by value + pass by reference. I need to use at least one of the functions in program to demonstrate pass-by-value; and at least one of the functions to demonstrate pass-by-reference with reference arguments. Here is what I got so far: #include <iostream> #include "Grosspay.h" #include "iomanip" using namespace std; double income(double hours, double payrate) { double grosspay = 0; double federaltax

ByRef seems to receive the value and not the reference in VBA 6.0

断了今生、忘了曾经 提交于 2019-12-20 01:46:04
问题 My little sample code Function AddNr(ByRef x As Integer) As Integer x = x + 2 AddNr = x End Function Sub test() Dim ana As Integer ana = 1 AddNr (ana) MsgBox ana End Sub should output 3 but outputs 1. To be more specific the ana variable is not modified after the call to the AddNr function. My environment is Microsoft Visual Basic 6.5 inside Excel 2007. 回答1: That should be: AddNr ana That is, no brackets. From Microsoft Help: Remarks You are not required to use the Call keyword when calling a

Undefined Behavior with the C++0x Closure: I

我只是一个虾纸丫 提交于 2019-12-19 20:37:24
问题 Consider the example: #include <iostream> #include <functional> // std::function #include <vector> // std::vector #include <algorithm> // std::for_each int main(){ auto adder = [](int x) { return [&](int y) { return x+=y; }; }; std::vector < std::function<int(int)> > vec; vec.push_back(adder(1)); vec.push_back(adder(10)); std::for_each(vec.begin(), vec.end(), [](std::function<int(int)> f){std::cout << f(33) << " ";}); std::cout << std::endl; } One expects the integers 34 and 43 43 and 76 ,

R: Pass data.frame by reference to a function

一个人想着一个人 提交于 2019-12-19 18:29:04
问题 I pass a data.frame as parameter to a function that want to alter the data inside: x <- data.frame(value=c(1,2,3,4)) f <- function(d){ for(i in 1:nrow(d)) { if(d$value[i] %% 2 == 0){ d$value[i] <-0 } } print(d) } When I execute f(x) I can see how the data.frame inside gets modified: > f(x) value 1 1 2 0 3 3 4 0 However, the original data.frame I passed is unmodified: > x value 1 1 2 2 3 3 4 4 Usually I have overcame this by returning the modified one: f <- function(d){ for(i in 1:nrow(d)) {