Skip to main content

Arguments

Functions can receive information from the outside through arguments.

You write arguments inside the parentheses that follow the function name. There is no fixed limit on how many you can add; just put a comma between them.

The function in the next example has a single argument, fname. Each call hands it a first name, and the function uses that name to print the full name.

Example

One argument, three calls:

Prompt
Write a function that takes fname as a parameter and prints the name followed by "Refsnes". Call it with "Emil", "Tobias", and "Linus".
Output: complete Python program

Parameters vs Arguments

People often use parameter and argument interchangeably, since both mean information passed into a function.

Seen from the function's side:

The parameter is the variable named between the parentheses of the function definition.

The argument is the real value that the function receives when someone calls it.

main.py
def my_function(name): # name is a parameter
  print("Hello", name)

my_function("Emil") # "Emil" is an argument

Number of Arguments

Normally the number of arguments in a call has to match what the function expects.

A function that needs 2 arguments must get exactly 2 of them.

Give it more or fewer and Python reports an error.

Example

This function needs 2 arguments and receives 2:

Prompt
Write a function that takes fname and lname and prints them together. Call it with "Emil" and "Refsnes".
Output: complete Python program

Example

This one needs 2 arguments but receives just 1:

Prompt
Show what happens when a function expecting 2 arguments is called with only 1. Define a function with fname and lname, then call it with only "Emil".
Output: complete Python program

Default Parameter Values

A parameter can have a default value. The function falls back on that value whenever a call leaves out the argument:

Example

Prompt
Write a function with a default parameter name = "friend" that prints a greeting. Call it with "Emil", "Tobias", no argument, and "Linus".
Output: complete Python program

Example

A country parameter with a default:

Prompt
Write a function with a default parameter country = "Norway" that prints "I am from" followed by the country. Call it with "Sweden", "India", no argument, and "Brazil".
Output: complete Python program

Keyword Arguments

Arguments can also be sent in the key = value form.

Because each value is labeled with its name, the order in which you write them no longer matters.

Python documentation often abbreviates Keyword Arguments to kwargs.

Example

Prompt
Write a function that takes animal and name parameters and prints information about a pet. Call it using keyword arguments animal = "dog" and name = "Buddy".
Output: complete Python program

Example

Prompt
Write a function that takes animal and name parameters. Call it with keyword arguments in reversed order: name = "Buddy", animal = "dog".
Output: complete Python program

Positional Arguments

Arguments passed without keywords are known as positional arguments.

Python matches them to parameters by position, so they have to be in the right order.

Swap them and you get a different outcome.

Example

Prompt
Write a function that takes animal and name parameters. Call it with positional arguments "dog" and "Buddy".
Output: complete Python program

Example

Changing the order gives a different result:

Prompt
Show what happens when positional arguments are passed in the wrong order. Call a function expecting (animal, name) with ("Buddy", "dog").
Output: complete Python program

Mixing Positional and Keyword Arguments

A single function call can use both positional and keyword arguments.

The one rule is that the positional ones go first:

Example

Prompt
Write a function with animal, name, and age parameters. Call it mixing positional and keyword arguments.
Output: complete Python program

Passing Different Data Types

Arguments can be of any data type: a string, a number, a list, a dictionary and so on.

Inside the function, the value keeps the same type it had when you sent it:

Example

Passing a list to a function:

Prompt
Write a function that takes a list of fruits and prints each one. Pass a list ["apple", "banana", "cherry"] to it.
Output: complete Python program

Example

Passing a dictionary to a function:

Prompt
Write a function that takes a person dictionary and prints the name and age. Pass {"name": "Emil", "age": 25} to it.
Output: complete Python program

Return Values

The return statement lets a function give a value back:

Example

Prompt
Write a function that takes x and y and returns their sum. Call it with 5 and 3 and print the result.
Output: complete Python program

Returning Different Data Types

A returned value can be of any data type, such as a list, a tuple or a dictionary.

Example

Returning a list:

Prompt
Write a function that returns a list of three fruits. Print each fruit individually.
Output: complete Python program

Example

Returning a tuple:

Prompt
Write a function that returns a tuple (10, 20). Unpack into x and y and print them.
Output: complete Python program

Positional-Only Arguments

A function can insist on receiving positional arguments ONLY.

You do this by adding , / after the arguments, as the first example shows.

Leave out , / and callers may use keyword arguments, even for a function written with positional arguments in mind, as in the second example.

Once , / is there, passing a keyword argument causes an error, which the third example demonstrates.

Example

Prompt
Write a function with positional-only parameter using /. Call it with a positional argument.
Output: complete Python program

Example

Prompt
Show that without /, a function allows keyword arguments. Define a function with name and call it with name = "Emil".
Output: complete Python program

Example

Prompt
Show the error when using keyword arguments with positional-only parameters. Define a function with name, / and call it with name = "Emil".
Output: complete Python program

Keyword-Only Arguments

To accept keyword arguments only, put *, before the arguments, as in the first example.

Without *,, callers may pass positional arguments even though the function was written with keywords in mind. The second example does this.

With *, in place, a positional argument causes an error, as the third example shows.

Example

Prompt
Write a function with keyword-only parameter using *,. Call it with name = "Emil".
Output: complete Python program

Example

Prompt
Show that without *,, a function allows positional arguments. Define a function with name and call it with "Emil".
Output: complete Python program

Example

Prompt
Show the error when using positional arguments with keyword-only parameters. Define a function with *, name and call it with "Emil".
Output: complete Python program

Combining Positional-Only and Keyword-Only

A function may use both kinds together.

Everything before / must be positional, and everything after * must be passed by keyword:

Example

Prompt
Combine positional-only and keyword-only arguments in one function. Use / and * to enforce argument types and return the sum.
Output: complete Python program