In the context of programming, what does Return do?

550 views

I’m trying to pick up computer programming yet again, and I’m not really understanding the use of Return. What is it? How would you use it?

In: Technology

5 Answers

Anonymous 0 Comments

When you call a function, you move from the current list of instructions being processed to a different list (the ones for the function). “Return” does exactly that- it returns you to the previous list of instructions.

If your function has return value, then it places that value in a special location (the stack) first so that the code that called it knows what the result was.

Here is what calling a function that adds two values (5 and 6) looks like in pseudocode:

1 Put value “5” on the top of the stack
2 Put value “6” on the top of the stack
3 Reserve a location on the top of the stack for the return value
4 Load the location for the next instruction after the function is finished (6) on the top of the stack
5 Go to instruction 8
6 Print the value on the top of the stack to the screen
7 End the program
8 Load the 3rd value on the stack
9 Load the 4th value on the stack
10 Add the two loaded values
11 Place the result in the 2nd location on the stack.
12 Go to the instruction listed at the top of the stack (6)

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