pass-by-value

Passing an integer by reference in Python

巧了我就是萌 提交于 2019-11-26 00:48:43
问题 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 pass the reference types of Integer , Long , etc. How can I pass an integer into a function by reference? What are the best practices? 回答1: It doesn't quite work that way in Python. Python passes references to objects. Inside your function you have

Is Swift Pass By Value or Pass By Reference

我怕爱的太早我们不能终老 提交于 2019-11-26 00:14:10
问题 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 reference? 回答1: Types of Things in Swift The rule is: Class instances are reference types (i.e. your reference to a class instance is effectively a pointer ) Functions are reference types Everything else is a value type ; "everything else" simply means

Is Java “pass-by-reference” or “pass-by-value”?

萝らか妹 提交于 2019-11-25 23:55:10
问题 I always thought Java was pass-by-reference . However, I\'ve seen a couple of blog posts (for example, this blog) that claim that it isn\'t. I don\'t think I understand the distinction they\'re making. What is the explanation? 回答1: Java is always pass-by-value . Unfortunately, when we pass the value of an object, we are passing the reference to it. This is confusing to beginners. It goes like this: public static void main(String[] args) { Dog aDog = new Dog("Max"); Dog oldDog = aDog; // we

Java pass by reference

丶灬走出姿态 提交于 2019-11-25 23:53:38
问题 What is the difference between this 2 codes: Code A: Foo myFoo; myFoo = createfoo(); where public Foo createFoo() { Foo foo = new Foo(); return foo; } Vs. Code B: Foo myFoo; createFoo(myFoo); public void createFoo(Foo foo) { Foo f = new Foo(); foo = f; } Are there any differences between these 2 pieces of codes? 回答1: Java always passes arguments by value NOT by reference. Let me explain this through an example: public class Main { public static void main(String[] args) { Foo f = new Foo("f");

Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?

江枫思渺然 提交于 2019-11-25 22:56:41
问题 class D { public static void main(String args[]) { Integer b2=128; Integer b3=128; System.out.println(b2==b3); } } Output: false class D { public static void main(String args[]) { Integer b2=127; Integer b3=127; System.out.println(b2==b3); } } Output: true Note: Numbers between -128 and 127 are true. 回答1: When you compile a number literal in Java and assign it to a Integer (capital I ) the compiler emits: Integer b2 =Integer.valueOf(127) This line of code is also generated when you use

Is it better in C++ to pass by value or pass by constant reference?

南楼画角 提交于 2019-11-25 22:27:15
问题 Is it better in C++ to pass by value or pass by constant reference? I am wondering which is better practice. I realize that pass by constant reference should provide for better performance in the program because you are not making a copy of the variable. 回答1: It used to be generally recommended best practice 1 to use pass by const ref for all types , except for builtin types ( char , int , double , etc.), for iterators and for function objects (lambdas, classes deriving from std::*_function )

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

独自空忆成欢 提交于 2019-11-25 22:17:34
问题 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 the new node BarList * newNode = malloc(sizeof(BarList)); // assign the right values newNode->val = bar; newNode->nextBar = list; // and set list to be equal to the new head of the list list = newNode; // This line works, but list only changes inside of this

Is JavaScript a pass-by-reference or pass-by-value language?

断了今生、忘了曾经 提交于 2019-11-25 22:10:19
问题 The primitive types (number, string, etc.) are passed by value, but objects are unknown, because they can be both passed-by-value (in case we consider that a variable holding an object is in fact a reference to the object) and passed-by-reference (when we consider that the variable to the object holds the object itself). Although it doesn\'t really matter at the end, I want to know what is the correct way to present the arguments passing conventions. Is there an excerpt from JavaScript

Passing Objects By Reference or Value in C#

…衆ロ難τιáo~ 提交于 2019-11-25 22:00:37
问题 In C#, I have always thought that non-primitive variables were passed by reference and primitive values passed by value. So when passing to a method any non-primitive object, anything done to the object in the method would effect the object being passed. (C# 101 stuff) However, I have noticed that when I pass a System.Drawing.Image object, that this does not seem to be the case? If I pass a system.drawing.image object to another method, and load an image onto that object, then let that method

Javascript by reference vs. by value [duplicate]

好久不见. 提交于 2019-11-25 21:38:56
问题 This question already has answers here : Is JavaScript a pass-by-reference or pass-by-value language? (30 answers) Closed 5 years ago . I\'m looking for some good comprehensive reading material on when Javascript passes something by value and when by reference and when modifying a passed item affects the value outside a function and when not. I\'m also interested in when assigning to another variable is by reference vs. by value and whether that follows any different rules than passing as a