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 Functions and Looping Returning Values

Mark Kohner
Mark Kohner
1,760 Points

confused about using return values

In this example, cost_per_person is returned. I am a bit confused, though. It is given a variable name inside of the function -- and not defined outside. Okay. Why is it given a variable name at all?

1 Answer

In this particular example, there is no reason other than making the code easier to understand for the student. A much simpler code would have been "return total / number_of_people", but using a variable name clarifies what that result is supposed to be.

That being said, you can totally use variables in a function. Functions are like any other piece of code you will write, you will need to store data and label them with explicit names. While a new variable isn't totally necessary in this example, they will become critical as your code becomes more complex.