directory
- A preface.
- 2.Python function definitions
- Calls to Python functions
- Pass arguments to Python functions
- 1. General arguments to Python functions
- 2. Default parameters of Python functions
- 3.Python functions have variable arguments
- The Python function returns the value return
- 6. Summary of Python functions
- Seven. Guess you like it
Recommended path for learning Python: Python Learning Directory >> Python Basics
A preface.
Python functions are snippets of code that can be called repeatedly. For example, the type/ID we have seen in previous articles are functions. These functions are built into Python. You don’t have to worry about how it’s done internally.
2.Python function definitions
In Python, to define a function, use the **def** statement, writing out the function name, parentheses, arguments in parentheses, and colon:, and then write the function body in an indent block. The return value of the function is returned by a return statement. If there is no return statement, None is returned by default;
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python function use. py @time :2021/3/29 08:00 @motto: A thousand miles without a small step, a small stream without a river or sea, the wonderful life of program needs to be accumulated with perseverance! """ def functionname( parameters ): "Function_suite return [expression]" def cusom_print(): print("hello world")Copy the code
Calls to Python functions
When code is executed line by line in a py file, if it encounters a function definition, the compiler automatically skips it, executes the code after the function, and calls the function if it wants to.
** Note: functions must be declared before they can be called. Python’s built-in functions such as print/type are already declared and defined in the Python compiler and can be called without worrying about how they are implemented internally. ** Example code is as follows:
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python function use. py @time :2021/3/29 08:00 @motto: A thousand miles without a small step, a small stream without a river or sea, the wonderful life of program needs to be accumulated with perseverance! """ def custom_print(): Print ("hello world") print("hello world") print("hello world") print("hello world") hello world hello world hello world '''Copy the code
Code analysis: At line 15, the compiler realizes that this is a function declaration, and instead of executing it, it automatically jumps to line 20, and the compiler realizes that line 20 is calling custom_print. It goes directly into the custom_print function and executes line 16/17/18 until the end of the function.
Pass arguments to Python functions
Functions can pass arguments externally, including parameters and arguments:
Print ("cusom_print: x={}". Format (x)) cusom_print(1) //1 is an argument to the function - used to pass arguments when calling the functionCopy the code
1. General arguments to Python functions
As a general rule, functions take several parameters by default, and how many arguments need to be passed when called externally. Example code is as follows:
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python function use. py @time :2021/3/29 08:00 @motto: A thousand miles without a small step, a small stream without a river or sea, the wonderful life of program needs to be accumulated with perseverance! """ def cusom_print1(x): print("cusom_print1 : x={}".format(x)) def cusom_print2(x,y): print("cusom_print2 : x={}".format(x)) print("cusom_print2 : y={}".format(y)) def cusom_print3(x,y,z): print("cusom_print3 : x={}".format(x)) print("cusom_print3 : y={}".format(y)) print("cusom_print3 : Z ={}". Format (z)) cusom_print1(1) Cusom_print2 (1,2) Cusom_print3 (1,2,3) x=1 cusom_print2 : y=2 cusom_print3 : x=1 cusom_print3 : y=2 cusom_print3 : z=3 '''Copy the code
2. Default parameters of Python functions
In Python function arguments, there are default arguments in addition to regular arguments, that is, default arguments have a default value,
- If the function is called without passing a parameter to the default parameter, the parameter takes the default value.
- ** If an external call passes a parameter to the default parameter, the value of this parameter should be equal to the parameter passed externally; 支那
Functions with default arguments are also called default functions. Example code is as follows:
#! Usr /bin/env python # -* -coding :utf-8 _*- "" www.codersrc.com @file :Python function use. py @time :2021/3/29 08:00 @motto: A thousand miles without a small step, a small stream without a river or sea, the wonderful life of program needs to be accumulated with perseverance! Print ("cusom_print4: x={}". Format (x)) print("cusom_print4: x={}". Format (x)) print("cusom_print4: x={}". y={}".format(y)) print("cusom_print4 : Z ={}". Format (z)) print("***"*20) cusom_print4(1) Cusom_print4 (1,4) cusom_print4(1,4,3)" x=1 cusom_print4 : y=2 cusom_print4 : z=3 ************************************************************ cusom_print4 : x=1 cusom_print4 : y=4 cusom_print4 : z=3 ************************************************************ cusom_print4 : x=1 cusom_print4 : y=4 cusom_print4 : z=3 ************************************************************ '''Copy the code
Note:
-
1. Each default parameter has a default value. If no external parameter is passed to the default parameter, the default value is used directly. Otherwise equal to the value of the externally passed argument;
-
2. Default parameters must be written at the end of function parameters.
Error writing
Def cusom_print4(x,y=2,z): print(” cusom_print4: x={} “. Format (x))
3.Python functions have variable arguments
*argc, **kargcs *argc, **kargcs *argc, **kargcs *argc, **kargcs
The Python function returns the value return
The return value of the function is optional, depending on your usage requirements. If the function does not return a value, None is returned by default, andFalseDifferent, it doesn’t represent 0, and it doesn’t represent an empty string, but it represents no value, which is null.
6. Summary of Python functions
- 1. The function must be declared before the call; otherwise, an error will be reported.
- 2. Note the default parameter writing method.
- 3. This function does not use return. By default, it returns None.
Seven. Guess you like it
- Python Configuration Environment
- Python variable
- Python operator.
- Python conditions determine if/else
- Python while loop
- Python break
- Python continue
- The Python for loop
- The Python string
- The Python list
- The Python tuple tuple
- Python dictionary dict
- Python conditional derivations
- Python list derivations
- Python dictionary derivations
Python function declarations and calls
This article is published by the blog – Ape Say Programming Ape Say programming!