If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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

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:

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.

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.

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.

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.