"""Hàm Xin_chao chào một người
với thông điệp cho trước """
print("Xin chào",ten + ', ' + loi_chao)
Xin_chao("Nam","đọc bài trên TipsMake.com vui vẻ nha!")
When running the above code, we will get the text on the screen as follows:
Xin chào Nam, đọc bài trên TipsMake.com vui vẻ nha!
As you can see, this Xin_chao()
function has 2 parameters. Therefore, if we call the function with two parameters, it will run "well" and will not encounter any error messages.
If the function is called with a different parameter number 2, the interpreter will display an error message. If you want, you can try calling the Xin_chao()
function with one parameter and no parameters.
# Error message when calling Xin_chao () function with 1 parameter
TypeError: Xin_chao () missing 1 argument positional required: 'loi_chao'
# Error message when calling Xin_chao () function without parameters
TypeError: Xin_chao () missing 2 required positional arguments: 'ten' and 'loi_chao'
Besides defining functions with a fixed number of parameters, you can use the methods we introduce below to create custom parameters for the function:
In Python, the parameter of the function can have a default value. We can provide this default value for a parameter by using the assignment operator ( =
) as the example below:
def Xin_chao(ten, loi_chao = "đọc bài TipsMake.com vui nha!"):
"""
Hàm này chào một người với thông điệp cho trước.
Nếu thông điệp không được cung cấp,
nó sẽ được mặc định là "đọc bài TipsMake.com vui nha!"
"""
print("Xin chào",ten + ', ' + loi_chao)
Xin_chao("Hải")
Xin_chao("Dũng","bạn khỏe không?")
When running the above code, we have the following output:
Xin chào Hải, đọc bài TipsMake.com vui nha!
Xin chào Dũng, bạn khỏe không?
In this function, the ten
parameter has no default value and is required when calling the function. The parameter loi_chao
has a default value of "đọc bài TipsMake.com vui nha!"
. Therefore, when calling the function you can provide this parameter or not. If the parameter loi_chao
, it will override the default value.
In a function, the number of parameters with default values is not limited, but once there are default parameters, all parameters on the right of the default parameter must also have default values. That is, non-default parameters cannot be behind default parameters. For example, if you define the Xin_chao () function as follows:
def Xin_chao(loi_chao = "đọc bài TipsMake.com vui nha!",ten):
Then will receive an error message:
SyntaxError: non-default argument follows default argument
When calling a function with several values, these values will be assigned to the parameters according to their location. For example, in the Xin_chao () function above, when calling Xin_chao("Dũng","bạn khỏe không?")
, The "Dũng"
value is assigned to the ten
parameter and the value "bạn khỏe không?"
assigned to loi_chao
.
Python allows calling functions by keyword parameters. When calling a function this way, the order (position) of the parameter may change, but the result is still valid and similar to other function calls.
# 2 tham số keyword
Xin_chao(ten = "Dũng",loi_chao = "bạn khỏe không?")
# 2 tham số keyword, thứ tự tham số thay đổi
Xin_chao(loi_chao = "bạn khỏe không?",ten = "Dũng")
# 1 tham số keyword, 1 theo vị trí
Xin_chao("Dũng",loi_chao = "bạn khỏe không?")
As you can see above, it is possible to combine parameters by keyword or location when calling a function. But remember that the parameters keyword must go after the parameter by location. If the parameter according to the position after the keyword parameter you will receive an error message:
Xin_chao(ten = "Dũng","bạn khỏe không?")
Error message:
SyntaxError: non-keyword argument after keyword argument
Sometimes, we do not know in advance the number of parameters that will be passed to the function, it is time to use the function call with the number of custom parameters.
In the function definition, we use an asterisk * before the parameter name to denote the parameter type, as the example below:
def Xin_chao(*ten_chao):
"""
Hàm này sẽ chào một danh sách người cho trước
"""
for ten in ten_chao:
print("Xin chào",ten)
Xin_chao("Hải","Hoa","Công","Sơn")
When running the above code we get the following result:
Xin chào Hải
Xin chào Hoa
Xin chào Công
Xin chào Sơn
Here, we call the function with many parameters. This parameter is "wrapped" into a tuple before being passed into the function. Inside the function, we use the for
loop to get all the parameters in tuple.
In the next lesson, we will learn about Python recursion.
Next lesson: Recursive function in Python
Last lesson: Python function is user defined
Don't forget to do your Python homework.