Beginner Chapter 74 · 25 min read · Module 6: Python Functions
Function Parameters
- After this chapter you can explain the difference between a parameter and an argument.
- You can write functions with zero, one or many parameters.
- You can name the main kinds of parameters: positional, default, *args, keyword-only and **kwargs.
- You can predict what happens when the number of arguments does not match the parameters.
- You can use a parameter as a local variable inside a function body.
A function that always does exactly the same thing is useful only a few times. Most functions become powerful when they can receive different input each time they are called. The names that receive this input are called parameters. They are written inside the round brackets of the def line.
Parameters let one function work on many different values. A function that adds two numbers does not need to know which numbers in advance. It only needs two names, and Python fills them when the function is called. This is the idea behind almost every useful function you will write.
In this chapter you will learn what parameters are, how they differ from arguments, and what kinds of parameters Python offers. The next chapters look at positional, keyword and default arguments in more detail. Here we build the big picture first.
Parameters and arguments
People often use the words parameter and argument as if they mean the same thing, but they are different. A parameter is a name in the function definition. An argument is the actual value you pass when you call the function. The parameter is like an empty box with a label, and the argument is the thing you put in the box.
def greet(name):
print("Hello,", name)
greet("Asha")
greet("Ravi")Here name is the parameter. The strings "Asha" and "Ravi" are arguments. Each call creates a fresh name that exists only while the function runs. When the function ends, name disappears.
Functions with different numbers of parameters
A function can have no parameters, one parameter or many. Separate several parameters with commas. When you call the function, Python must be able to give a value to every parameter that has no default. If you give too few or too many arguments, Python raises a TypeError.
def rectangle_area(width, height):
return width * height
print(rectangle_area(4, 5))
print(rectangle_area(2.5, 2))Parameters do not have a fixed type. The same function worked with integers and with a float. Python only checks that the operation inside the function, here multiplication, makes sense for the values you pass. This flexibility is called dynamic typing.
def add(a, b):
return a + b
try:
add(1)
except TypeError as e:
print(e)
try:
add(1, 2, 3)
except TypeError as e:
print(e)The error messages tell you exactly what is wrong. Read them carefully, because they name the function and say how many values were missing or extra.
The kinds of parameters
Python has several kinds of parameters. You will meet each of them in later chapters, but it helps to see them together once.
| Kind | Example | Meaning |
|---|---|---|
| Positional or keyword | def f(a, b) | Can be given by position or by name |
| Default | def f(a, b=10) | Has a value if the caller leaves it out |
| Variable positional | def f(*args) | Collects extra positional values in a tuple |
| Keyword-only | def f(*, key) | Must be given by name |
| Variable keyword | def f(**kwargs) | Collects extra named values in a dictionary |
| Positional-only | def f(a, /) | Must be given by position (Python 3.8+) |
When you combine several kinds in one definition, they must appear in a fixed order: normal parameters first, then defaults, then *args, then keyword-only parameters, then **kwargs. A default parameter can never come before a normal parameter without a default.
def describe(name, age=18, *hobbies, city="Unknown", **extra):
print(name, age, hobbies, city, extra)
describe("Asha")
describe("Ravi", 20, "chess", "music", city="Pune", job="teacher")Do not worry if this looks heavy now. Each part is explained in its own chapter. The important lesson is that the parameter list describes everything a caller may pass in.
Parameters are local variables
Inside the function body a parameter behaves like a normal variable, but it is local. Changing it does not change the variable that the caller used. For simple values such as numbers and strings, the original is never touched. For mutable objects such as lists, the function receives a reference to the same list, so changing the list in place is visible outside.
def change(number, items):
number = number + 1
items.append(99)
n = 5
lst = [1, 2]
change(n, lst)
print(n)
print(lst)append changes the shared object. This difference explains many surprising bugs.Good habits for choosing parameters
- Use clear names such as
priceandtax_rateinstead ofaandbwhen the meaning is not obvious. - Keep the number of parameters small. If a function needs more than four or five, consider grouping data in a dictionary or a class.
- Order parameters from the most important to the least important, and put optional ones last.
- Write a short docstring that explains what each parameter means.
Python syntax
def function_name(param1, param2):
bodydef function_name(param1, param2=default, *args, keyword_only, **kwargs):
bodyReal-world applications
- Passing a file name to a function that reads and cleans a file.
- Giving a calculation function the numbers it should work on.
- Sending a message text and a recipient to a function that formats emails.
- Passing configuration options such as size and colour to a drawing function.
Common mistakes
Fix: Write only names in the definition: def f(x). Pass the value 5 when you call f(5).
Fix: Count the parameters in the definition and give one value for each parameter that has no default.
Fix: Place all parameters without defaults first, then the ones with defaults.
Fix: Return the new value and assign it in the caller, for example n = change(n).
Important tips
- Name parameters after what they mean, not after their type.
- Read a TypeError message about arguments: it counts the missing and extra values for you.
- Use help(function) or a docstring to remind yourself what the parameters are.
Key points summary
- Parameters are names in the definition; arguments are the values in the call.
- A function may have any number of parameters, including none.
- Python has positional, default, *args, keyword-only, **kwargs and positional-only parameters.
- Parameters are local variables that exist only during the call.
- Mutable arguments can be changed in place by the function.
Exam and interview points
- Know the difference between parameter and argument, since it is a very common question.
- Parameters with defaults must come after those without defaults.
- Mutable arguments such as lists can be changed inside the function; rebinding a parameter name does not affect the caller.
- The order is: normal, default, *args, keyword-only, **kwargs.
Quick revision
- Parameter = name in def; argument = value in call.
- Wrong count of arguments gives TypeError.
- Parameters are local to the function.
- Defaults go after non-defaults.
- Lists passed in can be modified in place.
Frequently asked questions
What is the difference between a parameter and an argument?
A parameter is the name in the def line. An argument is the value you pass in the call. In def f(x) the name x is the parameter, and in f(10) the value 10 is the argument.
Can a function have no parameters?
Yes. You write empty brackets, like def hello(): and call it with hello(). Many useful functions need no input.
Do I have to declare the type of a parameter?
No. Python does not require types. You may add type hints like def f(x: int), but they are only hints and are not enforced when the program runs.
Why does Python say my function takes 2 positional arguments but 3 were given?
You passed more values than the function has parameters. Either remove one argument or add a parameter to the definition.
Practise this chapter
20 practice questions Take the chapter quiz