In the context of programming, what does Return do?

538 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

It tells the thing that’s interpreting or compiling your code that you want to exit the current function. If you just write ‘return’ that’s all that will happen, but typically you can also follow this up with some values that you want the function to spit out, i.e. the output of the function.

For instance, I could define this function (in python code):

def PlusTwo(x):
x+=2
return x

And then if in a command terminal (or some other script/function) I say:

> y = PlusTwo(3)

The resulting value of y will be 5.

You’ll often find return statements at the end of a function, but not necessarily. You can have different conditions under which you want the function to exit, with different outputs, and these might be found at different places in the function. However you nearly always find a return statement on the very last line of the function, because anything below that would be ignored. For instance, if I define a function like this:

def PlusTwo(x):
x+=2
return x
x+=7

the last line will never actually be executed, and so this function still just adds 2 to the value of x.

Some languages don’t require return statements – you just have to specify in the function definition what variables are your output variables. And then whatever the values of those variables are when the function exits will be your output. MATLAB for instance works this way. But others, like C or python, do use return.

Anonymous 0 Comments

In C, the value after the RETURN command what that function returns to the calling routine.

For example if you declared an int some_function(int a,b), then the function is supposed to return an integer value to the calling routine. Somewhere in the some_function, the return statement will assign this value.

EDIT: RETURN doesn’t end or exit the function. It simply assigns a return value.

Anonymous 0 Comments

Imagine you have a function called ADD(a,b) that, unsurprisingly, adds the value of a and b.

it could look something like this

int add(int a,int b){
return a + b;
}

and we’d use it like

int sum = add(4,6); //sum is now 10

Return in this context should be pretty obvious: it’s what returns the answer we’ve asked for.

Return does two things:

* it stops the current function that’s being executed and jumps back to whichever piece of code was running that called the function

* It gives back a value (usually the result of some calculation) that the code that called it can use in it’s own calculation.

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)

Anonymous 0 Comments

Computer works by breaking everything it does in small tasks. Let’s call those tasks as “functions”. Imagine functions as magic boxes, they could have zero, one or more inputs and return one output. Internally, those functions are also made combining another functions.

When a function returns, it could:

-Return a value. Like you’ve asked how much is 1+2? 3.
-Return it finished what it was doing. Call me “John”. Ok.
-Return that an error has occurred. How much is 1/0? Don’t know how to continue.