function
Functions are organized, reusable, code segments used to implement single or related functions.
In a python program, you need to define (declare) the function before using it, and then you can call it. As long as the necessary parameters are passed to the function in the form of the function definition, it can be called to complete the relevant function and obtain the function return result.
The syntax format of the definition function is as follows
def<Function name>(parameter list)<Function statement>return return value#This item is not required
By default, parameter values and parameter names are matched in the order defined in the function declaration
The code example is as follows
def hello():print("Hello World")hello()
The result is as follows
Hello World
>>>
The call of a function is to use a function. Defining a function is equivalent to giving the function a name, specifying the parameters contained in the function, and the code block structure.
After the basic structure of this function is completed, you can execute it through another function call or directly from the Python command prompt
The following is an example of function call
def printme( str ):
# Print any incoming string
print(str)return
# Call functions
printme("Hello")printme("World")
The output of the above example
Hello
World
In a python program, parameters are an important element of a function. When calling a function, parameters can be passed or not passed. In some cases, the number of parameters in a function cannot be determined. The parameters of the function will be explained in detail below.
The formal parameter represents a piece of information required by the function to complete its work, and the actual parameter is the information passed to the function when the function is called
The following are the formal parameter types that can be used when calling a function:
※ Required parameters
※ Keyword parameter
※ Default parameters
※ Variable length parameter
Required parameters are also called positional arguments and must be passed into the function in the correct order. The number when called must be the same as when declared.
The following example calls the printme() function, you must pass in a parameter, otherwise a syntax error will occur:
# Writable function description
def printme( str ):"Print any incoming string"print(str)return
# Call the printme function, no parameters will be reported
printme()
Example output results:
Traceback(most recent call last):
File "test.py", line 10,in<module>printme()
TypeError:printme() missing 1 required positional argument:'str'
Keyword parameters are closely related to function calls. Function calls use keyword parameters to determine the value of the passed parameter. The use of keyword arguments allows the order of the parameters in the function call to be inconsistent with the declaration, because the Python interpreter can match the parameter values with parameter names.
The following example uses parameter names when calling the function printme():
# Writable function description
def printme( str ):"Print any incoming string"print(str)return
# Call printme function
printme( str ="Hello World")
Example output results:
Hello World
The following example demonstrates that the use of function parameters does not require a specified order:
# Writable function description
def printinfo( name, age ):"Print any incoming string"print("first name: ", name)print("age: ", age)return
# Call printinfo function
printinfo( age=50, name="Tom")
Example output results:
first name: Tom
age:50
Recommended Posts