Results 1 to 10 of 10

Thread: "return" tutorial (per se)

  1. #1
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default "return" tutorial (per se)

    The Incomplete "return" Tutorial

    Author: Mr. W. / magicyte

    This tutorial's code examples are based completely on JavaScript and may not work in other coding languages.

    Prologue: "Why?"
    ----This "tutorial" (per se) will inform even the most "impaired" programmers (programmers that don't understand) what the 'return' keyword does and kinda how it works. This tutorial was created by a programmer. Programmers are humans, and humans make mistakes. In the Epilogue, reporting of errors, suggestions, and comments will be asked for. In other words, this 'tutorial' definately has mistakes. This 'tutorial' is based on general 'return' situations wherein a function is called and it then returns a value, in any case an integer (of any type), character, or string. Some may be thinking "Well, EVERYONE gets the 'return' keyword". This 'tutorial' is, once again, made for people who don't quite understand the 'return' keyword: what it does, or even how it works. Alas, the 'tutorial' begins:

    Chapter 1: "What?"
    ----What is 'return'? Well, it is a keyword that may be found in a function (most often, actually). This keyword will take the variable/inline character, string, or integer and return it, or send it back to the main function (on websites, back to the application's main runtime function [your internet browser]/on applications, back to the main() function). Also, once the return keyword is called, it returns the value, but it also exits the function and goes back to regular runtime execution/the main function. Examples:

    Code:
    // inline return values
    
    function getFive () {
    return 5; // returns integer 5 - will also exit function and go back to main function
    }
    
    function getHi_There () {
    return "Hi_There"; // returns string "Hi_There" - will also exit function and go back to main function
    }
    
    // variable return values
    
    function getFive () {
    var num = 5;
    return num; // returns 5 from variable num - will also exit function and go back to main function
    }
    
    function getHi_There () {
    var str = "Hi_There";
    return str; // returns "Hi_There" from variable str - will also exit function and go back to main function
    }
    It is also possible that you return functions that return things, as well:

    Code:
    function rt () { // will return "HI!!" - will also exit function and go back to main function
    return "HI!!";
    }
    
    function rthi () { // will return rt()'s return value - "HI!!" - will also exit function and go back to main function
    return rt();
    }
    You can also manipulate values in functions (I'm sure you could figure out how and why). And since everything in programming is numbers, you can return practically ANYTHING! Amazing!

    Chapter 2: "How?"
    ----Now, I see return values as inline pieces of code found in main functions. This is because you can return a function that just runs code! This will place it in the main function! Not exactly, but I think you know what I mean. This is how I think it works.

    An example. When you return an alert, it will alert something. You can try it by calling this function:

    Code:
    function rtatvl () {
    return alert("whatever you want here.");
    }
    I hope you understand what I am trying to say. I see return values being placed in to the main function in programs. All right, so when I see this in a function and the function is called:

    Code:
    function rtwtiwnt () {
    return alert("anything");
    }
    ... I think of this being in regular code:

    Code:
    alert("anything");
    Try this:

    Code:
    function rtwtvt () {
    return 111;
    }
    
    function rtalrtwtvt () {
    return alert("I am " + rtwtvt() + " years old.");
    }
    When rtalrtwtvt() is executed, I see code like this:

    Code:
    alert("I am 111 years old.");
    Please ask for clarification if this is vivid (I think it is, otherwise I wouldn't have said so). Since some members may not be Senior Coders who don't just get it, you may Private Message (PM) me by using the link in my signature at the end.

    Epilogue: "Help!"
    ----This tutorial is in need of a makeover! Post any errors, suggestions, and comments you may have that will help in the progress of this 'tutorial'.

    Projects 4 You:
    ----Here is/are some special project(s) that you can try out.

    1. Find the output of rtspecialrt()

    Code:
    function atip () {
    return "Hello.";
    }
    
    function btip () {
    return " My name i";
    }
    
    function ctip () {
    return "s Art.";
    }
    
    function rtspecialrt () {
    return atip() + btip() + ctip();
    }
    Link(s) I Didn't Use That Might Be Helpful:
    ----Here are/is some/a link(s) that I didn't use that might be helpful to use. In fact, I didn't use any resources except my brain:

    - The Return Keyword
    - return Keyword for Form Validation

    THE END
    Last edited by magicyte; 11-03-2008 at 10:01 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    This doesn't even give me half of what return does.
    Code:
    <script type="text/javascript">
       var price = function(a,b){
          var total = a+b;
          return total;
          return "Is the total.";
       }
       alert(price(5,7));
    </script>
    Explain why that won't work. That will atleast explain a bit more. The reason it won't work is because when returning a string or variable the whole function stops in response to the return. The return is ending the function.
    Jeremy | jfein.net

  3. #3
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    I see what you mean. It exits the function before the second return is started. I will add that in. Thanks.

    -magicyte

  4. #4
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Oh. I see you did edit it. But I can't seem to find out where you put my explanation(if you did), can you please point it out?
    Jeremy | jfein.net

  5. #5
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Sure thing. It is in the 1st Chapter at the very beginning.

    Chapter 1 ; Sentence 4

    -magicyte

  6. #6
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Oh, maybe I did post before you editted. My bad *runs and gets some oreos*
    Jeremy | jfein.net

  7. #7
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Haha!! Oreos. I love those things...

    Yeah. It really is okay. I kinda posted the thing and then started editing it. Bad thing was my IE7 internet browser failed on me and I had to start all over. I also lost my thought, so it isn't as good as I had it... Again, I am open for any suggestions.

    -magicyte

  8. #8
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    Edit: I forgot to add that you don't NEED to return a SPECIFIC value either. Just do this:

    Code:
    return;
    No SPECIFIC value returned, though it does return 'undefined'. (thanks to Nile + Twey)


    -magicyte
    Last edited by magicyte; 01-23-2009 at 12:56 AM.

  9. #9
    Join Date
    Jan 2008
    Posts
    4,167
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Just returning will return undefined. Mostly used to stop a function from executing rather than:
    Code:
    var a = function(){ return ; }
    alert(a());
    B/c that will alert:
    Code:
    undefined
    Jeremy | jfein.net

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It needs more than a makeover: it needs to be condemned, demolished, exorcised, rebuilt, and blessed to keep the vengeful spirit of the old one from ever returning. It's misguided, misleading, and in some places just plain wrong, and further confuses the issue by attributing basic language features to the return keyword. Calling another function to obtain a return value, for instance, is not special at all: the value supplied to return can be obtained from any expression, including a function call — and actually returning a function is something else entirely.

    A function is a 'black box' that takes some inputs and calculates an output. We call these inputs 'arguments' and the output a 'return value'.

    Code:
    3 -> +----------+
         | multiply | -> 6
    2 -> +----------+
    In Javascript, functions are values just like, say, numbers or strings. We call them using the 'call' operator, a balanced pair of parentheses (()), suffixed to the function object. Inside these, we can put zero or more arguments, separated by commas.

    Code:
    multiply(3, 2);
    The value yielded by applying the call operator to a function is the return value of the function.

    Code:
    2 + multiply(3, 2); // 8
    When the function is specified, using the return keyword inside a function terminates function execution immediately (so the rest of the function is never executed) and returns the value provided to return (or undefined if return is used on its own). All functions have an implicit return undefined at the end: if control reaches the end of the function, the function will return undefined.

    Code:
    function multiply(a, b) {
        return a * b;
        return 3; // never reached
    }
    Last edited by Twey; 01-21-2009 at 03:14 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •