What does return do in programming?

952 views

Primarily python

In: Technology

4 Answers

Anonymous 0 Comments

In Monopoly, you pull the card that says “go directly to jail. Do not pass go, do not collect $200 dollars.” You are instantly transported to another section of the gameboard and then follow the rules surrounding your transportation. You were “return”ed to jail.

Anonymous 0 Comments

return is how a function’s result is passed back to the caller.

Most programs are organized into functions (sometimes called procedures or subroutines). You might have the following command:

x = compute_average(list)

computer_average() might look like this:

int compute_average(List list)

total = 0

count = 0

for each item in list

total = total + item

count = count + 1

return total / count

In this case, return is saying “I’m all done, here is the average of the list you gave me.

Anonymous 0 Comments

When a function is called it’s pushed to the top of the call stack. The function does its work then when it hits a return statement it stores the return value in a special place, then the stack is popped and the parent function continues, with access to that special place the value is stored in.

Anonymous 0 Comments

It sends control of the program back to what called it and frees that memory space up. So when you call a function with a parameter, especially ones that are stored outside of your mainline, you need to return control back when you exit so that you can take advantage of the memory space