Python uses def to start the function definition, followed by the function name. Inside the brackets are the parameters of the function, and inside are the specific function implementation code of the function. If you want the function to have a return value, use return in the logic code in expressions.
Basic use
def function_name(parameters):
expressions
Instance
def function():print('This is a function')
a =1+2print(a)
Above we defined a function named function. The function does not accept parameters, so the brackets are empty, and the function code of the function follows. If you execute the script, you find that no output is output, because we only defined the function, but did not execute the function. At this time, we enter the function call function() in the Python command prompt. Note that the parentheses for calling the function cannot be omitted. Then the function code inside the function will be executed and the output result:
This is a function3
If we want to call the script in the script, we only need to add the function call statement at the end of the script
1 function()
Then when the script is executed, the function will be executed.
DEF function parameters
When we use the calling function, we want to specify the value of some variables to be used in the function, then these variables are the parameters of the function. When the function is called, just pass in.
Basic use
def function_name(parameters):
expressions
The position of parameters is the parameter of the function, which can be passed in when calling.
def func(a, b):
c = a+b
print('the c is ', c)
The parameter of a function defined here is two values, and the function of the function is to add the two parameters together. After running the script, call the function func in the Python prompt. If the parameter func() is not specified, an error will occur; output func(1, 2), pass a=1, b=2 to the function, and output the c is 3. Therefore, when calling a function, the number and position of parameters must be defined in accordance with the function. If we forget the position of the parameters of the function and only know the names of the parameters, we can specify the specific parameters func(a=1, b=2) during the function call, in this case, the position of the parameters will not be affected , So func(b=2,a=1) is the same effect.
DEF function default parameters
When we define a function, sometimes some parameters are the same in most cases, but in order to improve the applicability of the function, some optional parameters are provided. In order to facilitate the function call, we can set these parameters as default parameters. Then the parameter does not need to be explicitly given during the function call.
def function_name(para_1,...,para_n=defau_n,..., para_m=defau_m):
expressions
Function declarations only need to be given with the = sign where default parameters are needed, but note that all default parameters cannot appear before non-default parameters.
def sale_car(price, color='red', brand='carmy', is_second_hand=True):print('price', price,'color', color,'brand', brand,'is_second_hand', is_second_hand,)
A sale_car function is defined here. The parameter is the attribute of the car. In addition to price, colors, brand and is_second_hand all have default values. If we call the function sale_car(1000), it will be the same as sale_car(1000,'red ','carmy', True) is the same effect. Of course, you can also pass in specific parameters during the function call to modify the default parameters. By default parameters can reduce the complexity of our function call.
Basic knowledge points supplement:
**def **
Necessary function of custom function: def
Instructions:
def function name(Parameter 1, parameter 2, parameter...):
Function body (statement block)
return[return value]
Precautions
So far, this article on what def does in python is introduced. For more related what is def in python, please search for ZaLou.Cn's previous articles or continue to browse the related articles below. Hope you will support ZaLou more in the future. .Cn!
Recommended Posts