Question on return values of Function

Discussion in 'JavaScript' started by askscript, Nov 15, 2008.

  1. #1
    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.
     
    askscript, Nov 15, 2008 IP
  2. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    rene7705, Nov 15, 2008 IP
  3. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Without return y , the function : increment(x) is still being run, but it does not return the desired result to your upnum
     
    ads2help, Nov 15, 2008 IP
  4. askscript

    askscript Member

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Thanks guys...:)
     
    askscript, Nov 15, 2008 IP