ELI5. What does “return” do in programming? I read about it a lot and still dont understand the purpose.

342 views

ELI5. What does “return” do in programming? I read about it a lot and still dont understand the purpose.

In: 0

10 Answers

Anonymous 0 Comments

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’.

You are viewing 1 out of 10 answers, click here to view all answers.