pass-by-value

Why would I pass function parameters by value in C?

江枫思渺然 提交于 2020-01-14 14:22:47
问题 I am dusting off my C skills working on some C libraries of mine. After having put together a first working implementation I am now going over the code to make it more efficient. Currently I am on the topic of passing function parameters by reference or value. My question is, why would I ever pass any function parameter by value in C? The code might look cleaner, but wouldn't it always be less efficient than passing by reference? 回答1: In C, all arguments are passed by value. A true pass by

Is Ruby pass-by-value or pass-by-reference? [duplicate]

岁酱吖の 提交于 2020-01-12 05:57:06
问题 This question already has answers here : Is Ruby pass by reference or by value? (12 answers) Closed 5 years ago . I am basically a java developer. I am working in ruby for about an year. Unlike java, Ruby is a pure object oriented programming language. Here comes a doubt. Is it pass-by-value or pass-by-reference? Java works as pass-by-value: "When passing primitives, I see that the value is duplicated and passed to the method. But incase of Objects, the reference is duplicated and passed to

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

自古美人都是妖i 提交于 2020-01-08 09:29:42
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 4 years ago . 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 finding that every listener uses the value of results.length (the value when the for loop terminates). How can I add listeners such that each

Pass by Reference / Value in C++

点点圈 提交于 2020-01-08 08:44:10
问题 I would like to clarify the differences between by value and by reference. I drew a picture So, for passing by value, a copy of an identical object is created with a different reference, and the local variable is assigned the new reference, so to point to the new copy How to understand the words: " If the function modifies that value, the modifications appear also within the scope of the calling function for both passing by value and by reference " Thanks! 回答1: I think much confusion is

Python multiprocessing: object passed by value?

狂风中的少年 提交于 2020-01-04 06:04:49
问题 I have been trying the following: from multiprocessing import Pool def f(some_list): some_list.append(4) print 'Child process: new list = ' + str(some_list) return True if __name__ == '__main__': my_list = [1, 2, 3] pool = Pool(processes=4) result = pool.apply_async(f, [my_list]) result.get() print 'Parent process: new list = ' + str(my_list) What I get is: Child process: new list = [1, 2, 3, 4] Parent process: new list = [1, 2, 3] So, it means that the my_list was passed by value since it

Python multiprocessing: object passed by value?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 06:04:07
问题 I have been trying the following: from multiprocessing import Pool def f(some_list): some_list.append(4) print 'Child process: new list = ' + str(some_list) return True if __name__ == '__main__': my_list = [1, 2, 3] pool = Pool(processes=4) result = pool.apply_async(f, [my_list]) result.get() print 'Parent process: new list = ' + str(my_list) What I get is: Child process: new list = [1, 2, 3, 4] Parent process: new list = [1, 2, 3] So, it means that the my_list was passed by value since it

How I can make a string array interchange it's components with a swap function?

别来无恙 提交于 2020-01-04 04:43:07
问题 The problem is that this code won't interchange these 2 strings. I'm new to programming but I can tell that the problem is that swap function, but I do not know how to fix it. I tried to add strcpy instead of "=" in swap but that didn't worked. #include <stdio.h> #include <stdlib.h> void swap(char *t1, char *t2) { char *t; t=t1; t1=t2; t2=t; } int main() { char *s[2] = {"Hello", "World"}; swap(s[0], s[1]); printf("%s\n%s", s[0], s[1]); return 0; } 回答1: You want to use out parameters here, and

Updating pointers in a function

时光总嘲笑我的痴心妄想 提交于 2020-01-02 03:17:07
问题 I am passing a pointer a function that updates it. However when the function returns the pointer it returns to the value it had prior to the function call. Here is my code: #include <stdio.h> #include <stdlib.h> static void func(char *pSrc) { int x; for ( x = 0; x < 10; x++ ) { *pSrc++; } printf("Pointer Within Function: %p\n", pSrc ); } int main(void) { char *pSrc = "Good morning Dr. Chandra. This is Hal. I am ready for my first lesson."; printf("Pointer Value Before Function: %p\n", pSrc );

Pass-by-value (StringBuilder vs String) [duplicate]

◇◆丶佛笑我妖孽 提交于 2020-01-02 00:58:12
问题 This question already has answers here : Is Java “pass-by-reference” or “pass-by-value”? (86 answers) Closed 4 years ago . I do not understand why System.out.println(name) outputs Sam without being affected by the method's concat function, while System.out.println(names) outputs Sam4 as a result of the method's append method. Why is StringBuilder affected and not String? Normally, calling methods on a reference to an object affects the caller, so I do not understand why the String result

MATLAB variable passing and lazy assignment

自作多情 提交于 2020-01-01 12:07:55
问题 I know that in Matlab, there is a 'lazy' evaluation when a new variable is assigned to an existing one. Such as: array1 = ones(1,1e8); array2 = array1; The value of array1 won't be copied to array2 unless the element of array2 is modified. From this I supposed that all the variables in Matlab are actually value-type and are all passed by values (although lazy evaluation is used). This also implies that the variables are created on the call stack. Well, I am not judging the way it treats the