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 Raising Exceptions

What does split_check have ‘total’ in parenthesis but we never use total again?

I though we have to use the parameters in the parenthesis after defining a function, but we never see ‘total’ again???

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

I see total being referenced in the math.ceil() call in the return statement, line 7. Yes?

Thanks Chris for replying Chris!

But how does the code know what ‘total’ is? I can see an input for number_of _people, but not for total (only total_due).

Thanks

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

The connection is made in pairing the calling arguments from line 12, with the function’s signature containing the function’s parameters in line 4. All of these are merely labels that get assigned to point at objects.

The calling arguments total_due and number_of_people are labels defined at the check_please module level to point to the input results.

The receiving parameters total and number_of_people are local to the function and are labels that point to the same object as the calling parameter labels. This is why Python is called a “pass by object reference” language.

So the function’s local total will point at the same value as the passed in total_due points to.

This may be a deeper than you expected, but understanding this will help make much of Python make sense.

Happy to add more details as needed.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

An experiment to try:

  • add print(id(total_due)) after line 11 to see where that object is located in memory
  • inside the function, add print(id(total)) to see where the object pointed at by the local label is stored in memory

Voilà! Both labels point to the same object!!

Ok thanks Chris, this is super helpful.

You’re right, it’s slightly outside of my understanding at this point, but it has given me something to chew on for sure.

Thanks so much for you help!

I would really appreciate even more insight into Why total and Total due connect? I get that you can write code to connect it as you explained but my question is not about how to write the code but rather why it works? Is it because total is a word that is within total_due? Would the same apply if we named it apples and apples_due? or any combination of words for that matter so long as the first words match?

example : oranges and oranges_due

assuming we used the same structure but replaced the words total with oranges. and can we also swap out the word due? for example oranges and oranges_eaten. ?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

The specific text used as the label name is irrelevant. In generic terms, the function’s parameters could be named (labeled) as param0, param1, etc., and the arguments in the call to the function could be named arg0, arg1, etc., then the parser makes the one-to-one connection based on the order of the labels. This is done independently of the text used for the labels.