Parameters and Arguments

Functions

Parameters and Arguments

In this tutorial, we'll pass information into functions.

Functions become more useful when you pass information into them.

def greet(name):
    print("Hello,", name)

greet("Ada")
greet("Grace")

Output:

Hello, Ada
Hello, Grace

Parameter vs Argument

The parameter is the name in the function definition:

def greet(name):

The argument is the value you pass when calling the function:

greet("Ada")

Beginners often mix up these words. That is okay. The important idea is that values can go into a function.

Multiple Parameters

Functions can accept more than one value:

def show_score(name, score):
    print(name, "scored", score)

show_score("Mia", 8)

Arguments are matched by position. "Mia" goes into name, and 8 goes into score.

Keyword Arguments

You can also name the arguments:

show_score(name="Mia", score=8)

Keyword arguments can make calls easier to read, especially when there are several values.

Common Mistake

The number of arguments must match the function definition:

def greet(name):
    print("Hello,", name)

greet()  # error

Python expected one argument but received none.

Arguments Can Be Variables

Arguments do not have to be written directly into the function call. They can come from variables:

student_name = "Mia"
student_score = 8

show_score(student_name, student_score)

The function receives the values stored in those variables. This is one reason functions are so useful: they can work with different data each time.

Practice

Create a function called calculate_area(width, height) that prints the area of a rectangle.