View Full Version : "return" tutorial (per se)

11-03-2008, 02:43 AM
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:

// 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:

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:

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:

function rtwtiwnt () {
return alert("anything");

... I think of this being in regular code:


Try this:

function rtwtvt () {
return 111;

function rtalrtwtvt () {
return alert("I am " + rtwtvt() + " years old.");

When rtalrtwtvt() is executed, I see code like this:

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()

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 (http://jennifermadden.com/javascript/returnKeyword.html)
- return Keyword for Form Validation (http://jennifermadden.com/javascript/formReturn.html)


11-03-2008, 03:11 AM
This doesn't even give me half of what return does.

<script type="text/javascript">
var price = function(a,b){
var total = a+b;
return total;
return "Is the total.";

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.

11-03-2008, 03:16 AM
I see what you mean. It exits the function before the second return is started. I will add that in. Thanks.


11-03-2008, 03:19 AM
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?

11-03-2008, 03:22 AM
Sure thing. It is in the 1st Chapter at the very beginning.

Chapter 1 ; Sentence 4


11-03-2008, 03:26 AM
Oh, maybe I did post before you editted. My bad :o *runs and gets some oreos*

11-03-2008, 03:30 AM
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. :)


01-20-2009, 10:57 PM
I forgot to add that you don't NEED to return a SPECIFIC value either. Just do this:


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


01-20-2009, 11:06 PM
Just returning will return undefined. Mostly used to stop a function from executing rather than:

var a = function(){ return ; }

B/c that will alert:


01-21-2009, 03:08 PM
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'.

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.

multiply(3, 2);

The value yielded by applying the call operator to a function is the return value of the function.

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.

function multiply(a, b) {
return a * b;
return 3; // never reached