1

Does a method/function's call statement hold its return value similar to a how a variable name holds a value?

For example:

function Jump(height) {
    return height * 2;
}
Jump(3);       //Is the call statement 'Jump(3)' equal the return value of 6 similar to

var x = 3;  //how the variable 'x' is equal to the value of 3?

Edit: I gave the wrong impression by naming the variable and function the same name. I was purely using the variable as a comparison in understanding how return values are saved. I'll word/structure my question better next time.

2 Answers2

6

functions and variables share the same namespace. You defined the function Jump and then called it (but didn't actually assign the results of the function (what was returned)), so it returned 6 but then you didn't actually store it anywhere. Then you overwrote your function by redefining it as a variable with the same name, and just assigned it the value of 3

CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
  • If I don't manually store the function's return value inside a variable, how is the return value used as an argument for another function? i.e. run(jump(3)); Is the return value stored "behind the scenes"? –  Mar 10 '13 at 21:16
  • doing that is okay because you are passing the value to `run()`. The value will then be stored in the first arg inside run, eg: `function run(arg1) { alert('arg1); }` in that example, `arg1` now has the value and its stored there (but only during the execution and scope of the `run` function). – CrayonViolent Mar 10 '13 at 21:30
  • So calling a function is similar to a math calculation. i.e. 5 * 4 = 20 where 20 is its return value. If we wanted to multiply another number to that product, we wouldn't have to write: 5 * 4 = 20, 20 * 2 = 40 . We could simply write: 5 * 4 * 2 = 40. The expression (5 * 4) implicitly equals 20, so we can use the whole expression in place of the answer. It this a correct comparison functions and return values? –  Mar 11 '13 at 00:23
  • more or less, yes. But in general, a function doesn't have to return anything special (if you do not specify a return value, it will return nothing (null). You can just make a function perform some arbitrary action. In essence, functions are just a convenient way to wrap a label around some code so that you can execute some code over and over in multiple places, instead of typing it all out over and over – CrayonViolent Mar 11 '13 at 00:38
0

A little about expressions

Following your edit, it seems you're asking about expressions.

An expression is basically a snippet that has a value. In your example both Jump(3) and var x = 3 are expressions, as both have a value. In other words, Jump(3) is an expression with the value 6 and (x=3) is actually an expression which evaluates to 3.

The only difference between the two is that the first expression also assigns a value with the assignment operator (i.e. =), while the second expression is "just" a value. For that matter, in your example, the variable x is also an expression that evaluates to 3.

For more on this subject see MDN - Expressions and operators in Javascript

More to the point

This means that when you pass an argument to a function by-value, you're essentially passing the value of the expression. So in your example, Jump(x), Jump(3), Jump((x=3)) and Jump(Jump(1.5)) would all evaluate to 6.

Naturally, it's much less costly to store the value of common operations in a variable that would be quickly evaluated as an expression, than to have the calculation done each time the expression is evaluated.

Community
  • 1
  • 1
Boaz
  • 19,892
  • 8
  • 62
  • 70