pass-by-value

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

Trying to populate an array using a function in C++ [closed]

妖精的绣舞 提交于 2019-12-22 01:09:12
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . So, I'm trying to populate and print a small array using functions, however I've hit a bit of a roadblock. The code I have is: #include <iostream> #include <fstream> #include <cstdlib> #include <string> using namespace std; struct planes { string name; int seats; int range; int weight; }; int populate (planes

Python: What is the difference between Call-by-Value and Call-by-Object?

谁说我不能喝 提交于 2019-12-21 04:42:27
问题 Many people say that in Python arguments to functions are passed using a call-by-value model. As I understand it, it is not actually a call-by-value language, but a call-by-object or call-by-sharing model. What are the differences between a call-by-value model and a call-by-object model? What is an example in Python that shows how these models are different? 回答1: Saying that it is not pass-by-value is not correct. Semantically, it is pass-by-value, and one can demonstrate the semantic

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");

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

How to clone a node in the scene graph in JavaFX?

大兔子大兔子 提交于 2019-12-20 03:06:54
问题 I have a HBox with prefHeight = 70 // no prefWidth or any width... I also have a Pane with prefWidth = 50 // no prefHeight or any height... I just want to add multiple instance of the pane to the HBox using some loop. When I do add(pane) in the loop body it gives following error. Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = HBox[id=myHBox] I need to find way to clone the pane(as it passes by value). Can

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

How does Python referencing work?

天涯浪子 提交于 2019-12-19 08:09:07
问题 I am confused with Python referencing. Consider the following example: My task : To edit each element in the list d = { 'm': [1,2,3] } m = d['m'] m = m[1:] # m changes its reference to the new sliced list, edits m but not d (I wanted to change d) Similarly: d = { 'm': [1,2,3] } m = d['m'] m = m[0] # As per python referencing, m should be pointing to d['m'] and should have edited d In python everything goes by reference, then when is a new object created? Do we always need copy and deepcopy

Parameter passing in Java

和自甴很熟 提交于 2019-12-19 05:28:20
问题 I know that Java is always pass-by-value, but I do not understand why this works: public static void swap(int[] arr, int i, int j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } public static void main(String[] args) { int[] arr = {3, 4, 5, 6}; swap(arr, 1, 3); // arr becomes {3, 6, 5, 4} } And this does not work: public static void swap(int[] arr, int[] arr2) { int[] tmp = arr; arr = arr2; arr2 = tmp; } public static void main(String[] args) { int[] arr = {3, 4, 5, 6}; int[] arr2 = {1,

Why can't my Java method change a passed variable? [duplicate]

谁说我不能喝 提交于 2019-12-19 02:28:09
问题 This question already has answers here : Is Java “pass-by-reference” or “pass-by-value”? (86 answers) Closed last year . I was kind of baffled when I saw the following code did not work as expected. I thought Java always passed variables by references into functions. Therefore, why can't the function reassign the variable? public static void main(String[] args) { String nullTest = null; setNotNull(nullTest); System.out.println(nullTest); } private static void setNotNull(String s) { s = "not