Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Summary

In this lesson, you learned that:

  • A function is a named piece of code that you write once and can run as many times as you need. Thanks to functions, you don't have to repeat code, and when you change a function, the change applies everywhere it's used.
  • You define a function with the keyword def, a name, parentheses with parameters, a colon and an indented body: def print_area(width, height):. A definition alone doesn't run anything.
  • You call a function with its name and parentheses: print_area(5, 6). A function must be defined before it is called, otherwise you get a NameError.
  • A parameter is a name in the definition, an argument is a value in the call. Arguments go into parameters in order. A missing argument ends with a TypeError, unless the parameter has a default value: def print_price(price, currency="EUR"):.
  • return gives a value back to the place of the call, so you can use the call like a value: print(add(2, 3)), total = add(2, 3) * 2.
  • print() only shows a value; the program can't work with it. A function without return returns None. A function that calculates something should return the result.
  • return ends the function right away, and the rest of the function doesn't run. With conditions, the first return that runs wins.
  • Variables created inside a function, and its parameters, are local: they exist only inside that function. Values go into a function through its parameters and come out with return.
  • A function can take a list, go through it with a loop and return a result. The return goes after the loop, indented as much as the for.

In the next lesson, Dictionaries, you will learn to keep values that belong together, such as a person's name and age, in a single value.

Instructions

  1. 1

    Let's practice. Write a function cart_total with two parameters: prices (a list of prices) and discount with the default value 0. The function returns the sum of the prices minus the discount. Print cart_total(cart) and cart_total(cart, 50). The program prints 280 and 230.

  2. 2

    Write a function format_price with a parameter price that returns the price as text with the currency, for example "230 EUR" for 230. Use it to print the discounted total with a label: print(f"To pay: {format_price(cart_total(cart, 50))}"). Python first calls cart_total, and passes the value it returns to format_price. The program prints To pay: 230 EUR.

Back

10/10

  1. 1. Defining Functions
  2. 2. Calling Functions
  3. 3. Parameters and Arguments
  4. 4. Default Parameter Values
  5. 5. Returning Values
  6. 6. Print or Return?
  7. 7. Return Ends the Function
  8. 8. Local Variables
  9. 9. Functions and Lists
  10. 10. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.