2

I have this code:

var point = 2;
var change = function(){point = 5};

function makeChange(point,change){
   change();
   alert(point);
}

makeChange(point,change);

The idea is to give the user the ability to pass a condition for the value of point before using it in the function. But it doesn't take effect. When I add alert(point) into the change function it alerts 5 but then alerts 2 in the makeChange function.

user3187759
  • 187
  • 1
  • 16
  • You cannot change a variable passed into a function and have that affect the "original" variable – VLAZ Sep 27 '16 at 05:58
  • 1
    Possible duplicate of [Does Javascript pass by reference?](http://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference) – VLAZ Sep 27 '16 at 05:59
  • Note the local parameter `point` and the global variable `point` are two different variables. You are alerting the local `point` inside `makeChange`. Basically just change the name of your first parameter to something other than `point`. – Spencer Wieczorek Sep 27 '16 at 06:16

5 Answers5

1

you can return point in change()

var point = 2;
var change = function(){ return point=5; };

function makeChange(point,change){
 alert(change);
}

makeChange(point,change());
sheetal
  • 565
  • 6
  • 13
  • I don't understand. What's the "point" of passing "point" into `makeChange` to start with? –  Sep 27 '16 at 06:04
  • Then what is use of initializing point at this point ? point = 5; – Bharat Sep 27 '16 at 06:07
  • user wants to change the point value in change()..so I just did that – sheetal Sep 27 '16 at 06:26
  • @torazaburo there is no point :P But also worth noting that _this code is dangerous_. It ONLY works because both `change` and `point` exist in the same scope _and_ `change` uses the same variable. If any of those is not true, then it won't work. However, if both are true, then passing both to a separate function is _point_-less (heh) and it demonstrates lack of understanding of how functions work - the `point` passed into `makeChange` _is not altered_. Any assumption that it's related to the other one is wrong. – VLAZ Sep 27 '16 at 07:01
0

You can do something like this when you call function.

makeChange(point,function(){return change})
Bartłomiej Gładys
  • 4,525
  • 1
  • 14
  • 24
0

Your code is doing fine - in fact, if you do a alert(point); right after makeChange(point,change); you'll see that 5 is returned.

The confusing bit is that the number (namely, 2) is passed into the makeChange function by value instead of the reference (namely, point). Therefore, if you alert point inside makeChange, the originally passed value will be given: 2.

J S
  • 1,068
  • 6
  • 7
0

I think the problem with this code is confusion between local and global variable.

So when you are passing variable in function makeChange(point,change) its behaving like local variable for function makeChange now in the next line when you calling the change function its changing global point value. and then when you are doing alert since in function priority is local variable so you are getting 2.

You can use return approach for same.

var point = 2;
var change = function(){point = 5 return(point)};

function makeChange(point,change){
   point=change();
   alert(point);
}

makeChange(point,change);  
Roli Agrawal
  • 2,356
  • 3
  • 23
  • 28
0

The problem is because of your parameter having the same name as the global variable point. In the code below:

var point = 2;
var change = function(){point = 5};

function makeChange(point,change){
   change();
   alert(point);
}

The call to change() inside makeChange will assign the local parameter point defined here makeChange(point,change) to 5. Not the global point which will remain as 2.

A easy way to solve this is to not use the same name point in the paramater as the global variable. Otherwise due to scoping JavaScript will think you are taking about the locally defined point inside makeChange:

var point = 2;
var change = function(){point = 5};

function makeChange(Point,Change){
   Change();
}

makeChange(point,change);
alert(point);
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54