# Got a question about "return" function

I’m at KELVINTAPH but I still don’t really understand about the `return` thing ill be really happy if someone could explain to me.

1 Like

It can be used in function to return a variable or object.
For example:

``````function multiply(number, numberTwo){
// Multiply number with numberTwo
return number*numberTwo;
}
//or
function multiply(number, numberTwo){
// Multiply number with numberTwo
var result = number*numberTwo;
return result;
}
``````

If you then call the function:

``````var calculate = multiply(2,4);
// expected result: 8
``````

You should get 8 as a result in the variable ‘calculate’

KEEP IN MIND! Everything after return has been used will not run!

I’m still really confused but I understand a bit and I use python btw

I believe it’s the same usage for python (i might be wrong, haven’t used it yet, so if someone else could confirm it would be nice!)
In case it is:
return is used in a function to “send” out some information instead of just doing something.
You could for example calculate the health of all units within a function and return it, or what the price would be for xxx units of type yyy and use that function in an if statement to check if your gold is higher than the cost of the units.

Also: if you have it inside an if statement, it might never get to this “return” and will then never call it, if whatever is required isn’t fulfilled.
So you could also have probably something like this:

``````def maximum(x, y):
if x > y:
return x
else:
return y

# and use it like this:

higherHealthUnit = maximum(unit1.health, unit2.health)
``````
2 Likes

Thanks!
( 20 chars )

@Shurutsue Looks good to me for the Python version.

Thanks, but I just noticed, I probably named the variable wrong, because it’s not the unit that’s passed back, but the amount of health… but i guess it’s good enough to understand what return is for i believe.
@Trai_Leammookda, if there’re still questions floating around about it, others could probably explain it in a better fashion than me, so don’t be discouraged if you haven’t fully understood my weird explanation and request another! But if you did, I’m glad that I’ve been of help
But for learning purposes: It’s bet to test it out yourself and see what happens!

I see what you mean. This modification should return the unit back after testing the health in the function

``````def maximum(x, y):
if x.health > y.health:
return x
else:
return y

# and use it like this:

higherHealthUnit = maximum(unit1, unit2)
``````
1 Like

I’ve always used a simpler function to explain the concept of a `return`. Basically, as mentioned above, `return` is a way for a function to return a value to the place it was called from. For example take this simple function:

``````def addNumbers(num1, num2):
return num1 + num2
``````

This function does something very simple, it adds two numbers together and returns the sum. You would call this function like this:

``````sum = addNumbers(3, 2)
``````

You pass two numbers to the function, in this case `3` and `2`. The function adds them together and passes back the sum, which is `5` and that gets stored in the `sum` variable.

There are lots of awesome things you can do with `return` functions you’ll learn all about later, but that is the basic idea. It’s for passing information back from a function.

2 Likes