0
function test(a, b){
   a = a + 2;
   b = b + 5;
}

var a = 1;
var b = 2;

test(a, b);

console.log(a);
console.log(b);

This return 1 and 2, but i would like 3 and 7.

Is possible to make two operation in one function? This working if i use return, but how to use return to two operation?

live: http://jsfiddle.net/anCq6/

Dek Dekku
  • 1,441
  • 11
  • 28
  • 1
    I suggest you to read something about functions parameters, global variable and scope (especially in javascript) – steo Jun 23 '13 at 19:32

5 Answers5

2

The reason you are getting 1 and 2 instead of 3 and 7 is because there are two different a and b variables. There's the a and b you declared outside the function, and there is the a and b which represent the values you passed into the function. (Basically, the parameters declared in the function's parentheses are newly declared variables.)

If you want to change the external a and b, change your test function to the following:

function test(x, y) {
   a = x + 2;
   b = y + 5;
}

Or, alternatively, don't pass a reference into the function, so that the a and b in the inner scope refer to the same a and b as the outer scope:

function test() {
   a = a + 2;
   b = b + 5;
}
Derek Henderson
  • 9,388
  • 4
  • 42
  • 71
1

Just send it back as an object...

function test(a, b){
 a = a + 2;
 b = b + 5;
 return {a:a,b:b};
}

var a = 1;
var b = 2;

var test = test(a, b);

alert(test.a);
alert(test.b);

DEMO HERE

Kylie
  • 11,421
  • 11
  • 47
  • 78
0

This doesn't work because since numbers are passed by value and not by reference, you're modifying the local copies of those variables, however the ones in the outer scope remain unmodified.

If you remove the a and b parameters from your function, you'll get the behavior that you want since the a and b parameters that are being modified will be the ones in the outer scope.

What are references?

Here's a pretty decent answer - Javascript by reference vs. by value

In short, only objects and arrays are passed by reference. Although in reality it's more complex than this depending on how functions are defined and syntax, at this point you can assume that anything that's defined by calling new or the syntactic shorthands [] ( array ) and {} ( object ) are passed by reference. Other types like numbers and strings are passed by value.

Community
  • 1
  • 1
dherman
  • 2,832
  • 20
  • 23
  • You can't. Everything in javascript is passed by value. – georg Jun 23 '13 at 19:25
  • @thg435 No they're not. Objects and Arrays are passed by reference. See my updated answer. – dherman Jun 23 '13 at 19:30
  • @dherman: unfortunately, the answers on your link are not correct either. Objects are not treated "specially" when passed to functions - everything gets passed by value, no matter what type that value has. – georg Jun 23 '13 at 19:55
0

Another solution: because of how variable scope works in JavaScript, you can just remove the parameters of "test" function and it will work.

function test(){
   a = a + 2;
   b = b + 5;
}
Guillaume
  • 10,463
  • 1
  • 33
  • 47
0
function test(){
   a = a + 2;
   b = b + 5;
}

var a = 1;
var b = 2;

test();

console.log(a);
console.log(b);
user2240578
  • 87
  • 2
  • 6