Return help


#1

What is the point of a return if it stops the function from running??? What reason would I want a function to stop running just to have it return a value or anything else???

Is a return meant to stop a function that bears the same name of another function which can cause a function to have side effects which is often caused by code nesting?

I know that a return is part of a function. But when should I consider adding it into my code?
Do I call in a new variable and make it equal to the previous function name like the 2nd part of this code?:
var timeTwo = function(number){
return number * 2;};
var newNumber = timesTwo(4);
Console.log(newNumber);
I'm trying to look for patterns, but am having a hard time because I don't understand the need for a return


#2

You must use return when you want to get something back from a function, i.e. a value to assign to a variable:

//Here I make my function:
function myFunction(number) {
return (number * 2);
}

//Here I use the function's return to assign a value to a variable:
var someNumber = myFunction(8);

//someNumber has the value 16 assigned to it because myFunction(8) returned 16.
//**return** can also be used in if/else statements:

if (myFunction(8) === 16) {
console.log("Twice 8 is 16.");
} else {
console.log("Math is broken");
}

//this will print "Twice 8 is 16." to the console because (myFunction(8) === 16) evaluates to (16 === 16) which evaluates to true.

Hope this helps! There are also other ways to use return which you will learn about later on.


#3

But why does Javascript require you to use returns? What if I desided not to use them in my code?


#4

A Return function is used when you want to get something from a function, It could be any value, a string, integer, float etc.

Now when you define a function like this.

var Add(a,b) {
    return a + b; // Here the function adds the given parameters a and b respectively 
};

// Now you could use this function to get some value for a variable.
var additionof_1_and_2 = Add(1,2); // This will call the function, and it will add the given parameters and assign the returned value of the function to the given variable.

Here are more places where you can learn about this return statement.

Here are some simple links that will help you out:

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/return

http://www.w3schools.com/jsref/jsref_return.asp


#5

as @aiishe and @udayk8139 already said return serves the purpose of returning a value so that the function call will have this value and can be used just like this value.

The idea is that function are sub program that are not immediately executed. They just wait until they get a start command. This start command is a function call which interrupts the main code and begins with the function. The opposite effect is done by the return statement, it interrupts the function and returns to the main code. This can for example be used because the user made a wrong input or as said to return a value e.g.

function myFunction(number) {
    if(isNaN(number)){
        return "This is not a number";
    }
    return (number * 2);
}

Now here you can either stop the function returning a string if it is not a number or you stop and return the double of the input number. In case your function should not return something and your fine with it going on to the end you don't need to place a semicolon the interpreter will automatically add one at the very end of your function.


#6

But why does return stop a function from executing???????


#7

Because that's its job!? I don't really understand your question. The function call starts the execution of the function and the return stops it, normally after you've done what you wanted to do.


#8

"Return" helps you stop a function from executing, yes. Why? Really, it's just because that's what "return" does. Write "return", then a string or object or something, and that's what it should do.

It's just the "return"'s job, as @haxor789 said.:smiley:


#9

el que funciona es este
var porDos = function(numero) {
return numero * 2;
};
// llama a porDos acá
var nuevoNumero = porDos(8)
{
console.log(nuevoNumero);
};
porDos(9);


#10

Por favor, no acaba de decir la respuesta , porque tenemos que explicarlo, no acaba de dar la respuesta. Este es el traductor google , así que puede ser raro.


#11

I do not understand,
is not the answer ?


#12

Spanish:

No, es correcto. Sólo estoy diciendo que usted debe explicar antes de decir la respuesta para ellos.

-----------------------------------------------------------------

English:

No, it's correct. I'm just saying that you should explain before you say the answer for them.


#13

ah si, ese es el problema, llegue a esa solución por casualidad, no entiendo por que hay que declarar porDos como parametro nuevamente abajo, si el ejercicio te dice que puedes poner cualquier parametro alli y no ese especificamente. No lo explique porque vi que todos hablan ingles jeje


#14

Lol ok Gotcha . Recuerda que siempre puedes utilizar el traductor google práctico. :smiley:


#15

exacto, tratare así aprendo ingles tambien