i wrote this code for learning: The above code will return an alert value of 20. It is totally correct. However, if i remove the ** return y;** value, it will give an alert value of "undefined" The book tells me that after a function finishes executing its code, a return value can be passed back to the caller. I do not understand what does it mean by " pass back to the caller" and if i remove the return value, why it will give me a "undefined" value. I hope anyone here will be kind enough to explain this to me. Thank you in advance.
simple, a function always returns 'undefined', unless somewhere in that function you use the 'return' statement.. the 'caller' is the place in your code from where you call the function, in this case var upnum = increment(num) Code (markup): upnum is your caller.
Without return y , the function : increment(x) is still being run, but it does not return the desired result to your upnum