What is the Javascript "Return" function for?

Asked

Viewed 1,773 times

0

One of the functions I hate the most is the "Return" because I don’t understand what she does or didn’t do:

var quarter = function(number){
number / 4;
}


if (quarter(12) % 3 === 0 ) {
console.log("A declaração é verdadeira");
} else {
console.log("A declaração é falsa");
}

Well, if they execute this code in Java it occurs that "The Statement is false" 12 being divided by 4 of the quotient 3, is 3 dividing by 3, of the rest 0, which should give "The statement is true" but for a OBSCURE reason gives "The Statement is false". Now we’ll do another test:

var quarter = function(number){
 return number / 4;
}


if (quarter(12) % 3 === 0 ) {
console.log("A declaração é verdadeira");
} else {
console.log("A declaração é falsa");
}

If the attentive ones are the ones who will not notice, I put the "Return" before the variable "number", but for ANOTHER OBSCURE REASON , this example, if executed on your computer will give "The Declaration is true"! where in the first example it only gave "The Declaration is false!" as only a function changes something that should or should not happen. I want you to explain how the Return variable works what it does, what it fails to do, ALL its functions are its working examples. Besides telling me why example 1 is 2 are different. I appreciate it if you read it. but I really need your help help PLEASE! I hope to talk in the chat bye <3

  • 2

    return is an instruction.

  • Renan, but I don’t know what it does, if you read the text carefully, my words demonstrate it. I just wanted to know what it’s for. yes I made a mistake , in considering it a "variable" but, who never made a mistake or will stop making mistakes in life. instead of correcting, why not help?

  • My goodness, hahaha I just made a comment. In the description of the tag you have marked this question there is some information also: http://answall.com/tags/return/info

  • Well, I think the questions of stackoverflow do not accuracy the tags to be answered or asked (in source code only lets send questions with tags) but even without tags remains question :)

  • All right, all right.

1 answer

2


The return is not a function but an instruction (or command/functionality) of functions in many programming languages. What the return is to return a value that is the product of the function, and with this interrupts the rest of the function processing.

Among the reasons we use functions are chunks of complex logic or need to reuse the same code with different input data. Now the way the function returns what we want from these pieces of code (functions) is by using return.

When a function ends without having called a return the value it leaves or returns is undefined.

Take a look at these examples:

function foo(){
   // sem nada
}

function bar(){
    return 10;
}

console.log(foo()); // dá undefined
console.log(bar()); // dá 10

The difference, as in your example, is that Return returns the result of the function. And this result can be saved in a variable! Look at this example:

var a = bar();
console.log(a); // 10

gives 10 because the function return value has now been saved in the variable.

In your first example when you have at the end of the function only number / 4;, without the return, interpreter runs that command, or seka calculates the fourth part of number but does not pass this value back to the one who called the function. For that you need to return number / 4;

  • Well, Rgio as pecebi in your code you ultimalized in the second example "Return 10;" and in 1 there was nothing, what would do different? if you only put 10 without "Return"?

  • @Brunoterrolivehabbo with only 10 without return would-be undefined. Take a look here: https://jsfiddle.net/dd14h5z5/

  • Sergio. when you sent the link parsed is noticed something: that without the Return gets Undefined more with the Return it RETURNS 10 to the FOO function or not?

  • 1

    @Exact brunoterrolivehabbo. Return "is the voice of function" that says what goes on inside. No return nothing goes out of function, it does what it must do but gives no return.

  • So... should I use Return at the end of all my functions? is if I ultilize the "var" inside the function is it needs the "Return" will not modify the return?

  • @Brunoterrolivehabbo uses the return when you need the result of the function. Not all functions need re-turn, some call other functions as a way to continue the program, others change things on the page or write to the database. Depends on the case.

  • In case, some Return accuracy is some not right? then why only put the Return at the end?

  • @Brunoterrolivehabbo Return doesn’t have to be at the end. It puts where it works best. Usually the code is sequential and so the return come at the end, but put it where it’s needed. And read here also: http://answall.com/questions/2477/por-que-devo-usar-apenas-um-return-em-cada-fun%C3%A7%C3%a3o? Rq=1

  • Dear you helped me a lot congratulations, not even professionals will be able to explain me in a way that I can understand right, but you explained is I understood thank you so much for having lost a few minutes of your life with a being that did not understand something so simple <3

  • But only the last question: is my function have 2 parameters ( Or even more )? example: var Len = Function per(V,B){ Return V + B; } In this case, will Return give the result of the function in V or B? O.O

  • @Brunoterrolivehabbo when you use return V + B; is the same as return (V + B); namely the return exports the result of V + B.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.