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

Alpha Bet
Alpha Bet
277 Points

What is the benefit of defining a function in this scenario?

In Returning Values Craig defines a function split_check, but this doesn't seem to be particularly useful for the context. After defining the function you still need to define two additional variables (total_due and number_of_people) for the input values and then a third variable (amount_due) to get the output of the split check function so it can be put it into the print function.

It would be shorter to simply say:

import math

total = float(input("What is the total? "))
number_of_people = int(input("How many people? "))

amount_due = math.ceil(total / number_of_people)

print("Each person owes £{}".format(amount_due))

Is there a benefit to defining the split_check function that I'm not understanding?

1 Answer

Travis Alstrand
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travis Alstrand
Treehouse Project Reviewer

Good question Alpha Bet ! 👋

The main benefit of defining the split_check function is reusability.

If you ever need to calculate how to split a bill in other parts of the program, or even in a completely different program, you can just call split_check instead of rewriting the calculation each time. It also makes your code easier to read and maintain, since the logic is wrapped in a single, clearly named place instead of being buried inside other code.

In your example, the one-off version works fine for a quick script, but functions really shine when you want to avoid repeating yourself and keep your logic organized.

I hope this helps and makes sense! 😃