FAQ: Learn Python - What Good are Functions?

This community-built FAQ covers the “What Good are Functions?” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise What Good are Functions?:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

1 Like

Other FAQs

The following are links to additional questions that our community has asked about this exercise:

  • This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
  • Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
  • This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!

Not seeing your question? It may still have been asked before – try (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).

I cant see how the program links the bill to the meal_cost given later? I expected some kinde of linking them like (meal_cost = bill) first…?

def tax(bill):
“”“Adds 8% tax to a restaurant bill.”""
bill *= 1.08
print “With tax: %f” % bill
return bill

def tip(bill):
“”“Adds 15% tip to a restaurant bill.”""
bill *= 1.15
print “With tip: %f” % bill
return bill

meal_cost = 100
meal_with_tax = tax(meal_cost)
meal_with_tip = tip(meal_with_tax)

I was wondering the same thing but looks like nobody answered.

here you define a function with a parameter named bill:

def tax(bill):

and here you call the function and provide an argument:

meal_with_tax = tax(meal_cost)

this is known as positional arguments, python will pass the value of the first argument to the first parameter.

1 Like

Specifically, with positional parameters, only the position within the parentheses is important.

def my_function(x, y, z):
    # code here involving variables x, y, z
    return some_thing

# Now call it
apple = 7
orange = 8
pear = 10
print(my_function(apple, orange, pear))

The interpreter will assign the value 7 to the function parameter x, 8 to y, and 10 to z, solely due to the fact that x, y, and z have the same relative positions within the parentheses as do apple, orange and pear in the calling statement. The variable names matter not at all.

If the original function declaration had contained a variable name along with a default value, as
def tax(bill = 1.00), then the original poster’s thought would have been valid, and the variable names would count. More on that here.

1 Like

Thank you, this clears up a lot! :slight_smile:

Thank you so much for the well-explained example. Helped a lot.

In this exercise, return command was used, but it wasn’t explained beforehand what does it do.

You can fiddle with the code, see if you can figure it out :slight_smile:

you could consider to continue with the course, return will be explained

or do you already want an explanation now?

2 Likes

I think I will play around a little bit.

This is wrong. The tip should be calculated on the bill before tax. Think about it.

A very quick review of the first page returned by Dr. Google on the topic tilts about 60 - 40 on the side of post-tax tipping. At any rate, it’s certainly not a clear-cut majority either way

And a majority opinion, of course, is not necessarily right :P.

1 Like

So in this outcome we are tipping over the taxed amount.
It makes the end result 1% higher.
Is that correct?

OK I tried to apply what I had learned so far and ask for an input, then do the calculation on the input value. Even making sure it gets converted into an integer. like so:

def tax(bill):

  """Adds 8% tax to a restaurant bill."""

  bill *= 1.08

  print ("With tax: %f" % bill)

  return bill

def tip(bill):

  """Adds 15% tip to a restaurant bill."""

  bill *= 1.15

  print ("With tip: %02f" % bill)

  return bill

  

meal_cost = input("Meal Cost :")

meal_cost = int(meal_cost)

meal_with_tax = tax(meal_cost)

meal_with_tip = tip(meal_with_tax)

However when I try to run this code it just says “EOFError: EOF when reading a line”. Is this a bug in the interpreter? Every way I look at it this should just work… Anyone able to help me with this?

codecademy has problems with input(), its not allowed in all exercises, try running your code here:

Online Python Compiler (Interpreter)

Thanks - thought I hadn’t understood properly for a second, but the code seems to be just fine!