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

Parameters and Arguments

Our functions so far do exactly the same thing every time they are called. But to calculate the area of any rectangle, the function needs to know its width and height. Functions get such values through parameters:

def introduce(name):
    print(f"My name is {name}.")

introduce("Anna")
introduce("Tom")

This prints:

My name is Anna.
My name is Tom.

In the definition, name in the parentheses is a parameter: a variable that the function can use in its body. You don't create it with =; the parentheses in the definition create it. In the call, the value in the parentheses, "Anna", is an argument. When you call the function, the argument is stored in the parameter, and then the body runs. On the first call, name is "Anna", on the second call it's "Tom".

These two words are easy to mix up, so remember: a parameter is the name in the definition, an argument is the value in the call.

A function can have several parameters, separated by commas. The arguments then go into the parameters in order: the first argument into the first parameter, the second into the second, and so on:

def introduce(name, age):
    print(f"My name is {name} and I am {age} years old.")

introduce("Anna", 21)
introduce(21, "Anna")

This prints:

My name is Anna and I am 21 years old.
My name is 21 and I am Anna years old.

In the second call, the arguments are swapped, so 21 went into name and "Anna" into age. Python doesn't care what the values mean, only what order they come in.

Each parameter needs its argument. What if one is missing?

def introduce(name, age):
    print(f"My name is {name} and I am {age} years old.")

introduce("Anna")

The program ends with an error:

Traceback (most recent call last):
  File "<exec>", line 4, in <module>
TypeError: introduce() missing 1 required positional argument: 'age'

The error message says that the call is missing one argument, and it names the parameter that didn't get one: 'age'. Positional means that arguments go into parameters by their position, that is, in order.

Now we can solve the problem from the beginning of the lesson: one function that prints the area of any rectangle.

Instructions

  1. 1

    Create a function print_area with two parameters, width and height. In its body, print width * height.

  2. 2

    Below the function, call it with the arguments 5 and 6. The program prints 30.

  3. 3

    Call print_area two more times, for the other two rectangles from the beginning of the lesson: with the arguments 10 and 5, and then 1 and 5. The program then prints 30, 50 and 5, just like the long code at the beginning, but now each rectangle takes a single line.

Back

3/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.