Comments
tiltboi1 t1_j2f2c5o wrote
“return” essentially means that the function is done, and provides a way of giving an output. Usually a function is “called” by another function which needs its results, so the return keyword is saying we are done and ready to go return to the calling function.
The reason why we use a special keyword has to do with how functions are actually called. Each time a function is called, it’s parameters, local variables, etc are saved to RAM in a block called a stack frame. But during the computation of this function, you may need to call another function. The parameters and local variables are saved into another stack frame next to it. The return keyword initiates the procedure of deleting the current stack frame, and moving back to the older one.
TorakMcLaren t1_j2f2fdc wrote
Computers do lots of calculations. The vast majority of the time, you don't want to actually know the answer to a calculation. You only want to know a certain thing when a certain other thing happens. You use "return" to output the answer.
Let's say you want to look for Pythagorean triples, three numbers a, b and c such that a²+b²=c², where a, b and c are all positive whole numbers. You get the computer to run through different options for a and b. It will square them and add them together. Then, it takes the square root of that and checks if it's a whole number. If it is, then you've found one and you want to know about it. But the rest of the time, you don't care!
You'd maybe set up the code to try 1²+2²=1+4=5. It tries √5, but it's not a whole number.
Okay, now it tries 1²+3²=1+9=10. But √10 doesn't work either.
Then 2²+3²=4+9=13, but √13 ×
1²+4² fails.
2²+4⁴ fails.
But 3²+4²=9+16=25 and √25=5. So the computer has found a solution and returns 3,4,5.
eloel- t1_j2f2gw8 wrote
In most cases, it ends the current function and resolves in either a value or void/undefined depending on function type. Of course it depends on language and this isn't necessarily true for all languages, but it covers most.
AndroChromie t1_j2f3fdo wrote
A method can either do a function by the orders given from the caller and end or it can do a function that change a variable that can be returned to the caller.
Method not returning: "Open a box of eggs and smash them"....Done. The End.
Method returning: "Open a box of eggs and give one to me"...Here I'm returning one egg.
sterlingphoenix t1_j2f4vm1 wrote
Clarification: Which programming language? return
does different things in different languages. The answers you're getting are very specific to languages like C and it's derivatives, but this is NOT global.
tomalator t1_j2ft4hv wrote
When you have a bit of code that will be run over and over again with different inputs each time, you write a function. We need a start and an end to the function. Multiple ends are ok, but we always need to make sure we end it. If we don't don't the function, the code will just keep going, reading whatever is next after the function, thinking it's still in that function, and thats not ok. It could read anything, maybe even stuff you haven't written because it's not going through the code, but rather it goes through the memory of the computer and who knows what bits it will interpret as commands, what it will overwrite or what mischief it gets up to.
Let's write a function called divide
Divide(a, b)
If(b==0){
Return null
}
C = a/b
Return c
This is properly written code. If I instead left out that "return null" the function would try to do a/0, which it can't do.
Now lets write a function called multiply.
Multiply(a, b)
c = a * b
Return c
Here, if we left out return, the code would just keep running on and on through whatever memory address follow multiply. Let's say our function divide is kept there, then instead of returning a * b, it would return a/b
[deleted] t1_j2fvopr wrote
[removed]
FrankDrakman t1_j2f2u61 wrote
Imagine you had a smart little robot, Eff, who did nothing but tell you how long it would take to get to a destination in your helicopter, given the two starting points, but it took him an hour to figure it out. So you say "Eff, how long will it take me to fly from Albany to Albuquerque?", and Eff goes away, and returns an hour later with "7.4 hours, boss!". IOW, Eff(Albany, Albuquerque) returns 7.4.
In programming, f(x) can be any function you want it to be. In this case, it returned a number of hours, but it could return a name e.g. TOPSALES(Jan) could return 'Joe Smith' as the best salesman. The term 'return' comes because you give the function the input data, and it 'goes away', figures out the answer, and then 'returns' with the answer. Meanwhile, your main program is waiting, and can't move on until the function returns its answer. Thus, at that point, the function both returns with an answer, and hands control back to the main program.
Currently, this isn't a big deal, as most functions execute almost instantly but in the old days, you could wait five, ten, or more minutes waiting for a function call to 'return'.
surfmaths t1_j2f2asv wrote
You can create small pieces of code that we call "function". Those function take a few input that we call "arguments" or "parameters". They execute whenever they are meant to do... and usually return an output we call "result".
An example of function is "distance". It takes two coordinates as input and compute the distance between those coordinates and returns said distance.
So that does the return do in programming? It terminate the function it's in, and takes an optional argument which is the return value of the function.