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 Python Basics (2015) Logic in Python Fully Functional

Using return in Python

Is 'return' a function or is it a keyword? Does the placement of it matter when creating the function as along it is in there as long as somebody wants the value returned to the function of which is called?

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Yes, the placement matters. When the return statement (that's what we call it) is hit, the function ceases to execute. Most often you will see a return statement at the end of a function or at the end of some sort of if/else statement. Your function is going to run some code and do whatever it is you want it to do and then return the results of that code.

This may be a horrible analogy, but I'm going to give it a shot. I'm not a great cook, so I do sometimes use spaghetti sauce from jars. However, I sometimes have issues opening these. So I walk over to my husband and hand him the jar. You can think of this as calling a function. The jar would be the argument. Now the return is what I'm getting back. So if I hand him the jar and he hands it back before it's open, it doesn't help me. I need him to first (run the code) to open the jar and then hand it back :smiley:

Hope this makes sense and helps! :sparkles: