Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Diego Salas Polar
Diego Salas Polar
21,431 Points

Can anyone explain the word "return" in python. I don't seem to understand the logic behind it.

I don't really understand return in python, and could someone show me a example of how it works.

2 Answers

Steven Parker
Steven Parker
243,318 Points

Python is a unique language in many respects, but this is one thing where it's about the same as any other language.

When you "call" a function in your program, the computer temporarily stops taking new instructions from the place in your program where it found the function call, and starts taking them from the function code. When it encounters return, it stops taking instructions from the function and goes back to where it encountered the function call.

:point_right: So, in a sense, it means return to where you were before.

Optionally, it may carry back some value to be used by the part of the program that made the function call. But that's not always done. The "going back" part is.

:x: And just to be clear, it does not mean "equal to".

Its like in any other computer language it means equal to.

def add(a, b):
    print "ADDING %d + %d" % (a, b)
    return a + b

def subtract(a, b):
    print "SUBTRACTING %d - %d" % (a, b)
    return a - b

def multiply(a, b):
    print "MULTIPLYING %d * %d" % (a, b)
    return a * b

def divide(a, b):
    print "DIVIDING %d / %d" % (a, b)
    return a / b


print "Let's do some math with just functions!"

age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

Basically it's like you add this number and add this number. Then what do you do after that? You return it. Computers are dumb so you have to write instructions in detail for what the computer has to do. I hope that helps.