Questions tagged [pass-by-value]

pass-by-value is a "one way passing" so that modifications of the passed value inside the receiving function (or other entity like process, etc) are not returned back.

Filter by
Sorted by
Tagged with
-4 votes
0 answers
31 views

Can u pls explain why this is happening in Java? [duplicate]

public class Swap { public static void main(String[] args) { int a =10; int[] arr = {1,2,3,4}; change(10); change2(arr); System.out.println(a); for(...
S.Cristo's user avatar
17 votes
1 answer
2k views

If arrays are passed by reference, why should I use int(&)[]? [duplicate]

Consider: #include <iostream> using namespace std; void Change(int arr[3]) { for (int i = 0; i < 3; i++) { arr[i] = 1; } } int Test() { int arr[3] = { 0, 0, 0 }; ...
Francesco Derme's user avatar
-2 votes
0 answers
24 views

What is the different between pass by value and pass by reference in python with reference to function arguments [duplicate]

I read many articles saying python is pass by value and some say python is pass by reference. Can anyone explain what is really is with some example?
Ashutosh Das's user avatar
0 votes
2 answers
247 views

Weird behavior with std::string reference class member

Given this code: #include <iostream> class Foo { public: Foo(const std::string& label) : label_(label) {} void print() { std::cout << label_; }...
morpheus's user avatar
  • 18.8k
1 vote
3 answers
82 views

C program calling malloc results in bus error?

I have written this code to declare a pointer, p, and then dynamically allocate 4 bytes of memory for type int to then store some data in it. p should point to an address which holds the data 6 of ...
tekkyprograms's user avatar
0 votes
0 answers
24 views

How to avoid passing by reference inside of method (Java)? [duplicate]

I'm making my own Java matrix program for a linear algebra class and can't seem to stop passing my arrays by reference. I want each method to return a matrix (thus performing calculations based on the ...
Aidan K's user avatar
-3 votes
2 answers
89 views

C++ Pass-by-value, shouldn't the memory_addresses of the two variables be copied and binded into different places?

I'm practicing C++, and facing the Pass-by-value/reference topic. What I learned from BroCode is that, when we call a swap function by the parameters' value, both of their memory_addresses will be ...
ian's user avatar
  • 55
-1 votes
2 answers
566 views

Why did this python list appear to get passed "by value"? [duplicate]

I defined the following function: def myFunction(a): print(f'In function, initial a = {a}, {id(a)}, {type(a)}') a = a*10 print(f'In function, final a = {a}, {id(a)}, {type(a)}') return a When ...
Kanika's user avatar
  • 1
1 vote
4 answers
142 views

Question about call by value in C language

When I run this program, it outputs 10 20 (first snippets of code). #include <stdio.h> int x = 10; int y = 20; int main() { fun(x, y); printf("%d %d", x, y); } int fun(int x,...
Thilina's user avatar
  • 13
0 votes
1 answer
19 views

Some Issues in Tree Iteration in Python while the Tree Can be Dynamically Generated on the Fly

I am writing the following functionality. A root nodes is given with 2 set of indices (_indices_lhs and _indices_rhs), and fore every index that apperas in both the left hand side and right hand side ...
Cherry Toska's user avatar
-1 votes
1 answer
47 views

Can I directly traverse this linked list using just the head pointer?

I've just started DSA recently and this question may seems trivial, but big thanks to those who take it seriously. This is my program: #include<stdio.h> #include<stdlib.h> struct node{ ...
itzmartin_20's user avatar
0 votes
1 answer
72 views

C#: How to keep the global variable not changed when passed to a function?

I am developing an add-in for OneNote. In the sinppet below, a global variable AllPageXML is claimed first. Then the very first time when a window is loaded, AllPageXML is initinized with a value. ...
Valuex's user avatar
  • 104
2 votes
1 answer
39 views

Confusion about making binary search tree EMPTY in C

I want to define a function which makes binary search tree to empty with return-type void. These are my codes as below : _Node structure typedef struct _Node { int data; struct _Node* l_child; ...
bFur4list's user avatar
  • 133
0 votes
1 answer
55 views

Pass by value makes me lose last value of variable

I have two global scope variables, one stores a list of cars and the other creates an instance of Car Car car = Car('', ''); List<Car> cars = []; On each screen of my forms I assign some ...
Massau's user avatar
  • 47
4 votes
3 answers
99 views

How to delete[] decayed array? [duplicate]

How to delete an array declared with new if I don't have access to the original pointer x? Let's assume, I know the array size. For example, if I write the following code: void enlarge(int * x) { ...
user1079505's user avatar
1 vote
2 answers
85 views

Why single pointer as argument can't modify a string (in c)

By using a double pointer as argument in a function, the program below can modify the string, I understand. #include <stdio.h> #include <stdlib.h> #include <string.h> void display(...
Fary's user avatar
  • 67
0 votes
2 answers
87 views

passing structure to function in c language

can anyone help? why '&' is not required while calling a function in this program? but is thought that '&' is required in call by reference. #include <stdio.h> #include <stdlib.h> ...
Mohan Singh Bisht's user avatar
1 vote
1 answer
95 views

C++: nested lambda function refer same address of passed parameter

I studied lambdas and noticed some strange things when nesting lambda functions. int x{10}; // assume x has 0x1 address here cout << "SCOPE: x address: " << &x << endl;...
Vlad Zhukovsky's user avatar
0 votes
1 answer
209 views

How to pass a variable by value to another thread?

I need to launch multiple threads in Rust, where each thread receives its worker id (integer between 0 and 31 inclusively). How to do it in Rust? Here's what I have tried: struct PtrWrapper(*const u8);...
Serge Rogatch's user avatar
2 votes
1 answer
122 views

Stack allocation in a function call

When you call a function, arguments that are passed to the function are passed-by-value if they are not arrays or not deliberately passed-by-reference with the ampersand (&) symbol. For instance, ...
ConventionalProgrammer's user avatar
1 vote
1 answer
55 views

I don't understand fully how lists in c work

So I'm trying to add an element to the beginning of a list. The function prepend_list is at line 27. I'm trying to make it in two different ways, one returns a list back and one is a void function. #...
g1e2h3's user avatar
  • 15
0 votes
0 answers
22 views

is list parameter in java, pass-by-reference or pass-by-value? [duplicate]

I have the following code: class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> result = new ArrayList<List<Integer>>();...
Pritha Rahut's user avatar
0 votes
3 answers
106 views

How to understand secondary pointer?

i want to ask a question about pointer: void fun(const char** a) {...} 1. const char* b = nullptr; fun(&b); 2. const char** b = nullptr; fun(b); why use 1 but not 2? 1 is good, 2 donnot work
Yeatsflee's user avatar
0 votes
2 answers
65 views

Problems when i try to dynamically allocate an array of strings in C

I'am trying to figure out what's wrong with my code.When compiling, no errors or warning, just a segmentation fault. Can someone tell me what am i doing wrong?? #include "funciones.h" #...
elmarti's user avatar
  • 11
0 votes
2 answers
141 views

Struct variable passed by value vs. passed by pointer to a function

Let's say I have the following structure: typedef struct s_tuple{ double x; double y; double z; double w; } t_tuple; Let's say I have the two following functions: t_tuple ...
BobDeTunis's user avatar
0 votes
3 answers
120 views

What is the typical prototype for a deallocation function in C?

Looking at code on examples on StackOverflow I've noticed two distinct prototypes for object deallocation: struct foo *foo_create(int); void foo_free_v1(struct foo *); void foo_free_v2(struct foo **); ...
IPribec's user avatar
  • 236
0 votes
4 answers
150 views

C dynamic allocation of an array under struct inside a function

I have a struct that will contain some dynamic-allocated array. I have written the following code, and it works but I do not understand why it does work. #include <stdio.h> #include <stdlib.h&...
Sangjun Lee's user avatar
0 votes
0 answers
71 views

What is value, reference vs pointer and what these three example used to pass? [duplicate]

I am recently studying golang and realize that everything passed to a go function get a new copy of same type with different address? Is it something what we call pass by value? https://go.dev/play/p/...
Stan's user avatar
  • 642
0 votes
0 answers
19 views

Why does assigning a null reference in the calling method does not reflect in the caller [duplicate]

Can someone please explain the output of the below code. Why is line 5 not printing null but line 8 printing the updated values from changeValue method. I saw many posts about pass by value but they ...
user2186831's user avatar
0 votes
0 answers
36 views

Javascript object copy [duplicate]

I wanted to understand why when I make a copy of an object and reassign the "nestedObject" property from the obj2 (copy) is only modified in the obj2 (copy) and not in the original object. ...
Marcelo Alarcon's user avatar
0 votes
2 answers
200 views

c variables not visible to main. using C header files

I am trying to define variables in a C header file. Then do work on these variables in a function and use the new values of these variables in the main function. the code looks as follows: C header : ...
Lama RL's user avatar
  • 31
2 votes
1 answer
39 views

How do you pass in another object of the same type as a method parameter in python?

I am creating a class in python to represent a three-dimensional point (I know there are libraries to do this, it's more of an exercise in classes). One type of method I wish to have is one which can ...
Isaiah Blanks's user avatar
0 votes
0 answers
34 views

Passed Variables in macro change when called sub is run

I have a master_macro that calls 2 other macros: (sub)calc and (sub)transfer. The calc macro doesn't need any variables passed to it, but the other one does and when the master_macro calls the ...
Jeff's user avatar
  • 43
0 votes
3 answers
388 views

Multiple functions with the same name but their parameters are either constant or received by value or by reference

The title is a bit lengthy, but it's best explained by an example: Suppose we have the following functions in C++: void SomeFunction(int num) { //1 } void SomeFunction(int& num) { //2 } void ...
DannyBoy's user avatar
0 votes
0 answers
201 views

Android studio, view binding in other classes

So I've created a new class for button click orders. Everything works well, they got triggered but I get this error. "Attempt to read from field 'android.widget.FrameLayout com.example.turref....
LoreX's user avatar
  • 11
0 votes
0 answers
15 views

Javascript: variable reference is getting passed instead of value. How to pass value? [duplicate]

In the example given below, how to correctly pass the value of the iterator? const result = []; const a = [1, 2, 3]; const b = { x: { y: null } }; a.forEach(i => { const temp = Object....
Dev's user avatar
  • 1
0 votes
0 answers
27 views

function is changing array variable outside its scope -adding slice does not fix [duplicate]

So I have the below function: function filterSheet(sheetName, disputeStatus, disputeTypeArr, sourceSValues, removeColumnNumsArr){ let ss = SpreadsheetApp.getActiveSpreadsheet(); let sheet = ss....
Connor Stokes's user avatar
0 votes
1 answer
152 views

How to pass a matrix as an argument without changing its original values in Python [duplicate]

I am trying to pass a matrix (list of lists) as an argument to a function. I want to keep the original matrix values intact. How can I implement this in python? I have tried to copy the matrix in a ...
Saurabh Damle's user avatar
1 vote
1 answer
170 views

malloc'd pointer inside struct that is passed by value

I am putting together a project in C where I must pass around a variable length byte sequence, but I'm trying to limit malloc calls due to potentially limited heap. Say I have a struct, my_struct, ...
Jeffrey Bonde's user avatar
-1 votes
1 answer
48 views

Capturing reference as value in Lambda expressions

int TestMethod(const std::map<std::string, std::string>& map) { auto m1 = [map=map]() { // is map here captured as reference or value ? std::cout << map.at("test"...
rstr1112's user avatar
  • 308
1 vote
1 answer
97 views

Semantics of a class within a struct

I'm reviewing data structures and came across something that I never realized when it comes to Linked Lists. This specific example is for Linked Lists, but the concept I think will be largely around ...
CodeBoxRoll's user avatar
0 votes
1 answer
47 views

Flutter : Dropdown reusability

I need to reuse a dropdown widget, So I created a method. I am calling like this : buildDropDown("Blood Group", bloodGroups, _groupSelected), The method definition: DropdownButtonFormField2&...
Mohammed Safvan's user avatar
0 votes
0 answers
29 views

How to enact pass by reference in python as we do in C/C++?

x,y = 30,40 def swap(x,y): x = x+y y = x-y x = x-y print(x,y) #Here it swaps but as soon as function ends they go back to their previous values swap(x,y) print(x,y) #still ...
SKumar's user avatar
  • 43
1 vote
1 answer
46 views

Passing argument 2 of 'fibonacci' from incompatible pointer type

#include <stdio.h> #include <stdlib.h> //Fibonacci void fibonacci(int n, int *fibs[n]){ int i; *fibs[0] = 1; *fibs[1] = 1; printf("%d %d ", *fibs[0], *fibs[1])...
somebody's user avatar
0 votes
1 answer
63 views

Clarification regrading Java pass-by-value

I am solving a coding question where we need to remove all the sub-trees of a binary tree that only has 0 as its value . Question link https://leetcode.com/problems/binary-tree-pruning/ The solution ...
Aman Kumar Sinha's user avatar
0 votes
1 answer
91 views

Copy vs ref benchmark: When are objects NOT passed in registers?

I heard that small objects are passed in CPU registers on function calls. I tried to search the maximum of when this is not anymore the case. I know that string_view is passed in registers. But what ...
glades's user avatar
  • 3,866
3 votes
1 answer
250 views

How Do Vectors Pass By Value?

If I have created a vector object, an instance that has a size of 24 Bytes (on my machine) will be allocated. I have read that a vector object contains (roughly speaking) two elements: Pointer points ...
Osama Alkurdi's user avatar
1 vote
2 answers
86 views

Linked List Search not working (SIGBUS error and memory leak)

i made a C program implementing linked lists but it won't work... everything i implemented was taken from websites or stackoverflow and it seems to be working for everyone, still does not work for me. ...
user avatar
2 votes
0 answers
22 views

PHP pass by value and mutation [duplicate]

In the PHP, we have a copy-on-write behavior. Why the following code has an unexpected result? As you can see passByValue function mutate the $value array. Since it's a pass by value, I expect that ...
A.A's user avatar
  • 3,182
0 votes
0 answers
49 views

Not able to understand pass-by-value in Java with block scope

I'm learning the basics of Java and currently understanding how call-by-value works. I do know that when you pass in primitives, the copy of the value is being made hence the original value is not ...
retro_coder's user avatar

1
2 3 4 5
24