FAQ: Learn Python - What Good are Functions?

faq

#1

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!


#2

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).


#3

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)


#4

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


#5

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.


#6

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.


#7

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


#8

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