When using "return" in Jscript is it's purpose if used in a function to write something back ? Instead of explaining here is a piece of code.

function apple() {
    var one = "Hello";
    var two = "Orange";
    var total = One+Two;
    return total;
}
document.write(total);

Shouldn't the write command be writting the combined scrings of both variables one & two based from the return value ?

Recommended Answers

All 16 Replies

You can use return to send back data to the caller.

Your code should be updated to this...

 function apple() {
     var one = "Hello";
     var two = "Orange";
     var total = one + two;
     return total;
 }
 document.write(apple());

Shouldn't the write command be writting the combined scrings of both variables one & two based from the return value ?

No, because "total" is a local variable not available in global context of the "document.write".
Executing the "apple" function will of course return the assignment value of the variable 'total' but since there is nothing there to capture it, it will evaporate.

More elaborate approach would be to

var apple = function() {
     var one = "Hello";
     var two = "Orange";
     return one + two;
 }

var total = apple();

document.write(total);
writes: "HelloOrange"

Write the name of the function in the document.write, sweet.

I assume that is the main purpose of the 'return' command ?

I assume that is the main purpose of the 'return' command

Troy's example really shows a better example of the role of return. A function is called and the function returns the output and that output is assigned to a variable. Once its in the variable you can continue to work with the variable in the rest of your block of code.

commented: an even better explanation :) +9

Troy is making an annoymous function, then calling the variable as a function.

In reality I'm not calling a variable as a function, I'm calling the function by the name of the variable.

They are both references::pointers to the assigned function object.

A note should be made that: There is a slight behavioral difference between them. With a function assigned to a variable, you cannot do something like:

var total = apple();
document.write(total);

var apple = function(){
     var one = "Hello";
     var two = "Orange";
     return one + two;
 }

as in:

var total = apple();
document.write(total);

function apple () {
     var one = "Hello";
     var two = "Orange";
     return one + two;
 }

Regards.

Adding a function to a variable before reading the function. In your second example.

Adding a function to a variable before reading the function. In your second example.

the function return; capturing the function return onto a variable the value of which you will be feeding the document.write command.

I assume that this prettymuch rounds up the answer on return statement

The main purpose of a return most of the time, is to return a local variable in a function ? I could've placed a write command in the function and it would have done the same thing, or are these options I have within the language ?

The main purpose of a return most of the time, is to return a local variable in a function ?

The purpose as I know is to send back data/output data. I subroutine is not required to have output. If you want the function to have output, you use return.

I could've placed a write command in the function

Yes sure if that is the objective of your function. For your functions to be reusable they should be very specific to what they do.

Of course you could have left the return statement out of your script syntax and most probably be writing a smaller footprint function to produce the (same) meant result.

You were shooting at (if we understood it well enough) discussing the use and the meaning of it.

The meaning of a certain expression is a subject to its context of its syntactical position. There is no main purpose on a dynamic and a live language like JavaScript.

The return statement is able to return any JavaScript value with no restriction;
Every possible JavaScript object or literal can be returned with return statement, including complex functions or their result which will also come from a return value.

The return statement is most often used on writing functional methods. In other cases it used to conditionally exit the execution process before the remaining statements of the function body are executed.

• The return statement has no meaning outside of a function , in that case an exception message will be thrown: "'return' statement outside of function"

It can also be used to conditionally break, that is, to exit a loop.

No language tells you how to use it. So it is up to the user's natural talent and obtained skill -to use it the way he/she means to formulate express and communicate a certain meaning.

Have fun

commented: always providing awesome examples and explanations! +12

Thanks for the help, I understand better how return works, I knew how it worked in loops, I wasn't sure it's purpose outside of loops :) If return stops the execution of a code block within a function, I assume false does the opposite ?

That's to say the least an incorrect asumption.

This function:

function doesNothing(){
    return;
    var one = "Hello";
        var two = "Orange";
        var total = one + two;
        return total;
 }

is equivalent to:

    function doesNothing(){
        return;
     }

or even:

    function doesNothing(){
     //empty
     }

because a function without return also evaluates to undefined.

To better ilustrate this, suppose you have something like
( 2 + 2 ) in the program stream. After the expression evaluation parens will contain a pure integer ( 4 ). (In fact there will be no parens either but we are using them for illustration purposes).

Analogous to that, we can have a function ( doesnNothing() ), which after parsing time leaves parens containing ( undefined ).

That's in case the function either doesn't have a return statement, or the return statement has nothing to return. In all other cases it contains the returned value.

Be it a Boolean true or false, regardless... it doesn't change the return behavior.

So, are we getting there yet? :)

You use return if you want to return something back to the main script. In other words:

function() {
var one = "salt"
var two = "pepper"
var total = salt + pepper;
return(total);
}

the words saltpeper will be returned to the script, for which you could, as you used in one of your examples; use a write command and write the string to the screen/monitor !

correct, with the addition that the return statement will also exit the function at the exact point of insertion canceling all other statements that may follow!

Perfect, I understand now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.