return value; is my arch nemisis!

Discussion in 'JavaScript' started by extrickster, Jan 28, 2011.

  1. #1
    Okay, I really wanna become friends with return; but no matter what I do it won't let me... we have been enemies for the longest time now. :(

    What I am asking for is some clarification of this function... what is it used for, why do you use it, and what is the purpose of it? :confused: The book I am reading uses it a-lot but never has took anytime to explain it.

    I have seen it used in multiple manners...

    return true;
    return false;
    return "Hello, World!";
    return Math.floor(Math.random() * 15);

    But sadly I understand none of those... Please take a minute to explain what the return function does... and please use English.

    Thank you. :)
     
    extrickster, Jan 28, 2011 IP
  2. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #2
    I think you picked the wrong book - Maybe you should try a book that seems easier for you to understand

    return in javascript and in many other languages is used mainly to tell a function:
    "Hey, now you are done with calculations, you solved what you were made for, please return this value to the program that called you"

    Some examples of functions:
    A function that returns a number:
    function product(x,y){
      return x*y;
    }
    Code (markup):
    A function that return a boolean (true or false:
    function is_even(n){
      if(n%2 == 0)  // In English: if the remainder of n/2 equals 0
        return true;
      else 
        return false;
    }
    Code (markup):
    This is like in maths: f(x) = 2x + 4
    Throw a 5 inside f(x) it will return 14

    I hope you get the idea
     
    nabil_kadimi, Jan 28, 2011 IP
  3. captain_kuro

    captain_kuro Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    beside giving a value, return also act as function terminator. Let's see:
    function say_things() {
      alert("I am seen");
      return;
      alert("I am not seen");
    }
    Code (markup):
    When you run
    say_things()
    Code (markup):
    you will only see an alert box with the text "I am seen".
    Because the interpreter (whatever runs this code) see "return" before the second "alert()", it stops executing the function until that point.
    Still confused?
     
    captain_kuro, Jan 29, 2011 IP
  4. captain_kuro

    captain_kuro Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    beside giving a value, return also act as function terminator. Let's see:
    function say_things() {
      alert("I am seen");
      return;
      alert("I am not seen");
    }
    Code (markup):
    When you run
    say_things()
    Code (markup):
    you will only see an alert box with the text "I am seen".
    Because the interpreter (whatever runs this code) see "return" before the second "alert()", it stops executing the function until that point.
    Still confused?
     
    captain_kuro, Jan 29, 2011 IP