1

I'm new to programming and am unable to find any good explanation on parameter/arguments, how they work under the hood. For eg:

function changeStuff(a) {
        return a = a * 10;
      }

var num = 10;
console.log(changeStuff(num)); //prints 100
console.log(num); //prints 10

When I call this changeStuff function, how does javascript put variable num into parameter - a? Does it do something like a = num under the hood?

I'm sorry if its a bad or a dumb question.

Rohan Sharma
  • 324
  • 3
  • 12
  • The argument `a` will have the value of `num` which is `10`. so the function will calculate `10 * 10` and returns the result. – Thielicious Nov 13 '18 at 15:30
  • 2
    you would need to see compiled code rather than this interpreted code to understand it more, as you don´t even deal with registers here. If it works same way as c++ (probably similar), when calling a function, function parameters are pushed onto current stack. Then the function reads values from the stack according to the amount and function reads/writes those values – juvian Nov 13 '18 at 15:32
  • 2
    Possible duplicate of https://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference – Peter Pajchl Nov 13 '18 at 15:33
  • @juvian that should be an answer. Great explanation. – Paul Nov 13 '18 at 15:33
  • @Thielicious, so inside javascript engine it is saying `num = a`? It is just abstracted away from us? – Rohan Sharma Nov 13 '18 at 15:34
  • More like `a = num` as the outer scope variable is being referenced basically – Thielicious Nov 13 '18 at 15:38
  • oh, okay i think I understand better now . Thanks alot guys ! – Rohan Sharma Nov 13 '18 at 15:43
  • @Paul maybe I went a bit too much under the hood though ^^ – juvian Nov 13 '18 at 15:43
  • @juvian yeah .. a little too under the hood .. but its my mistake, i should have said inside the javascript engine rather than under the hood .. though, im pretty sure what you have answered is pretty much correct if I could understand it :P – Rohan Sharma Nov 13 '18 at 16:24

2 Answers2

3

You would need to see compiled code rather than this interpreted code to understand it more, as you don´t even deal with registers here.

Assuming it works same way as c++ (probably similar), when calling a function, function parameters are pushed onto current stack. Then the function reads values from the stack according to the amount and function reads/writes those values.

In compiled code, there will not exist such thing as 'a' variable. Only a limited amount of registers are available, so a will actually be one of those. Before assigning that, the value from the register will be pushed onto the stack, so that when function ends, the register can go back to its previous value for the running code that might have been using it before calling the function.

A bit of literature on the subject

juvian
  • 15,875
  • 2
  • 37
  • 38
0

Javascript copies the reference to the function a * 10 to the variable a in this case. So a is a * 10 then and so a * 10 will evaluated and returned.

  • 1
    Except `10` is a primitive value and those are `passed by value` in javascript; if they were passed by reference as you say the `num` var would be modified by the function. – Peter Pajchl Nov 13 '18 at 15:44
  • a * 10 is a function. a will get a reference to the function a * 10. a function is an (complex) object. If you have a complex type, even if it contains primitive types, it will be passed by reference. num will be passed to a in the function a * 10 as value, a in the return statement will get the reference to the function. –  Nov 13 '18 at 16:20
  • thanks, i guess i have to look up pass by value/reference thoroughly, maybe that will help me understand what going on – Rohan Sharma Nov 13 '18 at 16:30