English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
InUser-defined functionIn this article, we have learned about defining functions and calling functions. If you are not clear about the way functions are called, it will lead to unpredictable errors. Here is an example.
def greet(name, msg): "This is a greeting function" There are two parameters: name, msg print("Hello", name + ', '' + msg) greet("Monica", "Good morning!")
Output result
Hello Monica, Good morning!
The function greet() has two parameters.
Since we have called this function with two arguments, the function runs normally and there are no errors.
If the interpreter is called with different numbers of arguments, it will display an error message. Below is the call to this function, demonstrating the error messages thrown when only one argument is used and when no arguments are used, after running them.
>>> greet("Monica") # Only one parameter is passed TypeError: greet() missing 1 required positional argument: 'msg'
>>> greet() # No parameters are passed TypeError: greet() missing 2 required positional arguments: 'name' and 'msg'
So far, functions have a fixed number of parameters. In Python, there are other methods to define functions that can take a variable number of arguments.
Below, we introduce three different forms of this type.
In Python, function parameters can have default values.
We can use the assignment operator (=) to provide default values for parameters. Here is an example.
def greet(name, msg="Good morning!"): "" This function greets the person who provides the message. If no message is provided, then it defaults to "Good morning!" "" print("Hello", name + ', '' + msg) greet("Zhang San") greet("Li Si", "how are you recently?")
Output result
Hello, Zhang San, good morning! Hello, Li Si, how are you recently?
In this function, the parameter name does not have a default value, so the name parameter must be passed when calling this function (mandatory).
On the other hand, the default value of the parameter msg is "Good morning!". Therefore, no value needs to be passed when calling the function. If a value is provided when calling, it will override the default value.
Any number of arguments in a function can have default values. However, once default arguments are present, all arguments to the right must also have default values.
This means that non-default arguments cannot follow default arguments. For example, if we define the function header above as:
def greet(msg = "Good morning!", name):
We will receive the following error message:
SyntaxError: non-default argument follows default argument
When we call a function with some values, these values are assigned to the parameters based on their position.
For example, in the above function greet(), when we call it as greet("Bruce", "How do you do?") the value "Bruce" is assigned to the parameternameAnd "How do you do?" has been assigned to msg.
Python allows the use of keyword arguments to call functions. When we call functions in this way, the order (position) of the parameters can be changed. Subsequent calls to the above function are valid and produce the same result.
# 2keyword arguments greet(name = "Bruce", msg = "How do you do?") # 2keyword arguments, parameter order changed greet(msg = "How do you do?", name = "Bruce") #1 positional arguments, 1 keyword arguments greet("Bruce", msg = "How do you do?")
As we have seen, we can mix positional arguments with keyword arguments during the function call. However, we must remember that keyword arguments must follow positional arguments.
Placing a positional argument after a keyword argument will cause an error. For example, the function call is as follows:
greet(name="Bruce","How do you do?")
It will cause an error:
SyntaxError: non-keyword arg after keyword arg
Sometimes, we do not know the number of parameters to be passed to a function in advance. Python allows us to handle this situation by calling a function with an arbitrary number of parameters.
In the function definition, we use an asterisk (*) before the parameter name*)indicates such parameters. This is an example.
def greet(*names): """The objects greeted by this function are All the people in the tuple list.""" # Name is a tuple with parameters for name in names: print("Hello", name) greet("Monica", "Luke", "Steve", "John")
Output result
Hello Monica Hello Luke Hello Steve Hello John
Here, we called a function with multiple parameters. These parameters are wrapped into a tuple before being passed to the function. Inside the function, we use a for loop to retrieve all the parameters.