What does return return? / Hebrew

What does return return? / Hebrew

Keyword return are inseparably neighbors with functions in most programming languages. But why do we call this instruction exactly return, why is it “return”? It is often thought that return is used here in the sense of returning the value of a function. The logic is as follows: we want the function to return some value, so the command is called return.

But if we think about it, we find it strange. Because “return” sounds like we first gave this function something, and then it returns something to us. When I order food at home, the service does not say let’s return I’m going home to you.” Any word would be better here: “we will create”, “produce”, “generate”, but definitely not “return”. After all, the essence of the order is that I don’t have this food now, And I want to get it. And with “return” it sounds as if I will give it to them first.

Actually is the key word return is not used in programming because it can return something. The ability of a function to “return” a value (or, as we understand, it will be more accurate to say, for example, “create”), passing it to the place of the function call, is only a side action, not a mandatory behavior.

Team return was originally used to refer to the sequence of code execution. When you call a function, execution of the program jumps to another location in its source code. For example, if you call a function foobar, then the execution of the code stops here, and the computer jumps to where the body of the function is located. After the execution of the code in the body, this return to the place of the function call occurs.

This is the idea behind the keyword return. And the fact that the function can also randomly transfer values ​​is an optional bonus. And the fact that in most programming languages ​​it was combined into one command is just a tribute to convenience. That is, when we write, for example, return 42then in expanded form it should be read as return AND pass 42.

Keyword return insidious – it does not mean exactly what it is usually taken for. And although such inaccuracy is unlikely to create problems for anyone, it is useful to know how it is actually arranged.

I heard this remark that struck me on Rahim Davletkaliev’s Thoughts and Methods podcast (Episode #53 “Unobvious Difficulties for Newbies, Part 2”, 09:36-13:00) and was surprised at my ignorance in such a seemingly an obvious question. I hope it was informative not only for me.

Related posts