When I Change a Parameter Inside a Function, Does It Change For the Caller, Too

When I change a parameter inside a function, does it change for the caller, too?

Since you're using C++, if you want xc and yc to change, you can use references:

void trans(double x, double y, double theta, double& m, double& n)
{
m=cos(theta)*x+sin(theta)*y;
n=-sin(theta)*x+cos(theta)*y;
}

int main()
{
// ...
// no special decoration required for xc and yc when using references
trans(center_x, center_y, angle, xc, yc);
// ...
}

Whereas if you were using C, you would have to pass explicit pointers or addresses, such as:

void trans(double x, double y, double theta, double* m, double* n)
{
*m=cos(theta)*x+sin(theta)*y;
*n=-sin(theta)*x+cos(theta)*y;
}

int main()
{
/* ... */
/* have to use an ampersand to explicitly pass address */
trans(center_x, center_y, angle, &xc, &yc);
/* ... */
}

I would recommend checking out the C++ FAQ Lite's entry on references for some more information on how to use references properly.

Why can a function modify some arguments as perceived by the caller, but not others?

Some answers contain the word "copy" in a context of a function call. I find it confusing.

Python doesn't copy objects you pass during a function call ever.

Function parameters are names. When you call a function Python binds these parameters to whatever objects you pass (via names in a caller scope).

Objects can be mutable (like lists) or immutable (like integers, strings in Python). Mutable object you can change. You can't change a name, you just can bind it to another object.

Your example is not about scopes or namespaces, it is about naming and binding and mutability of an object in Python.

def f(n, x): # these `n`, `x` have nothing to do with `n` and `x` from main()
n = 2 # put `n` label on `2` balloon
x.append(4) # call `append` method of whatever object `x` is referring to.
print('In f():', n, x)
x = [] # put `x` label on `[]` ballon
# x = [] has no effect on the original list that is passed into the function

Here are nice pictures on the difference between variables in other languages and names in Python.

Does call by value of a function change the original value of the parameters?

Yes, this function will change the original values in the matrix.

Call by value means the arguments to a function are copied onto the stack. The function can then modify these copies without changing the values outside. But this is never the case with arrays.

When you have an array as a parameter, it will always be converted to a pointer. This means

void powerup(char mat[9][9])

and

void powerup(char (*mat)[9])

are the same.

Can I change the passed value inside function?

Actually the argument you pass to function is pass by value this means that even when you change the argument value it doesn't reflect to actual value...

Here, from w3Schools - JavaScript Function Parameters

Arguments are Passed by Value

The parameters, in a function call, are the function's arguments.

JavaScript arguments are passed by value: The function only gets to
know the values, not the argument's locations.

If a function changes an argument's value, it does not change the
parameter's original value.

Changes to arguments are not visible (reflected) outside the function.



Objects are Passed by Reference

In JavaScript, object references are values.

Because of this, objects will behave like they are passed by
reference:

If a function changes an object property, it changes the original
value.

Changes to object properties are visible (reflected) outside the
function


This from mdn - function deceleration | Thanks @Ivar

Primitive parameters (such as a number) are passed to functions by value; the value is >passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.

If you pass an object (i.e. a non-primitive value, such as Array or a user-defined object) as >a parameter and the function changes the object's properties, that change is visible outside the function

change a functions argument's values?

Consider a slightly different example:

public class Test {

public static void main(String[] args) {
boolean in = false;
truifier(in);
System.out.println("in is " + in);
}

public static void truifier (boolean bool) {
if (bool == false) {
bool = true;
}
System.out.println("bool is " + bool);
}
}

The output from running this program would be:

bool is true
in is false

The bool variable would be changed to true, but as soon as the truifier method returned, that argument variable goes away (this is what people mean when they say that it "falls out of scope"). The in variable that was passed in to the truifier method, however, remains unchanged.

c++ change function's variable argument

In order to change the arguments, you would have to take references:

bool verifyStudent(string& id, string& name, int& grade, int& points, string& type) 

Although I'd say that function is not verifyStudent as much as verifyAndCorrectStudentIfNeeded.

Why use address of variable to change the value of a variable?

  • increment_int is pass by value. If the function increment_int changes
    the value of its parameter, it is only reflected in its local copy.
    The caller doesn't see the change.
  • increment2_int is pass by reference. You pass the address of x rather
    the value of x to this function. This function changes the value of
    at the specified address which is reflected on the caller side too.

Update (int) variable in C inside a function

stage is passed by value to changeStage. stage = 1 only changes the local value of stage in changeStage, not the value of stage in main. You have to pass a pointer instead:

while (stage != 0) {
changeStage(&stage);
}

void changeStage(int *stage) {
*stage = 1;
}

Modify a variable inside a function

The modifyTest function is essentially creating a local, function-level variable called s; that variable only exists within the scope of the function, so modifying it will not effect external scope.

If you want to modify the external scope, you would not use an argument:

var test = "This is a simple test";
function modifyTest(){
test = "modified test text";
}
console.log(test); // This is a simple test
modifyTest();
console.log(test); // Modified test text

Note that you can modify an object passed by reference, so you can modify something's properties:

var o = { test: 'This is a simple test' };
function modifyTest(x){
x.test = 'modified test text';
}
modifyTest(o);
console.log(o.test); // modified test text

You could even pass in the name of the property you wish to modify:

var o = { test: 'This is a simple test' };
function modifyTest(x, name){
x[name] = 'modified test text';
}
modifyTest(o, 'test');
console.log(o.test); // modified test text


Related Topics



Leave a reply



Submit