1. Concept of function
A code block with independent functions is called a function
2. Function
In structured programming, function is the most basic encapsulation of code, which is generally encapsulated according to function. Encapsulation is to develop programs that can be reused, reduce the redundancy of code with the same function, improve the efficiency of writing code, and make the code more concise and readable.
3. Function definition and call
1. Define the format of the function:
def Function name(): Code block
Code example:
# Definition of function def printof(): print("---------------------") print("Life is short, I want to learn Python") print("---------------------") # Function call printof()
4. Function parameters
After learning this, I believe you are familiar with the concept of function, but I don't know if you have a question: how can I transfer data to function? This requires the use of function parameters, which pass the data to the function to obtain the desired results
1. Define functions and calls with parameters
# definition def Function name(Parameter 1,Parameter 2): Code block # call Function name(Parameter 1,Parameter 2)
Code example:
# -*- codeing = utf-8 -*- # @Time :2020/9/19 22:58 # @Author:qzp # @File : demo3-1.py # @Software: PyCharm # Definition of parametric function def add(a,b): c = a + b print(c) # Call parameterized function add(3, 5)
5. Function return value
It solves how to pass the value into the function, but how to obtain the result calculated by the function. If you want to return the result to the caller in the function, you need to use return in the function
Code example:
# The parameter function is defined and the result is returned to the caller def add(a,b): c = a + b return c # Call the parameterized function and get the result # Mode 1: print(add(3, 5)) # Mode 2: #Call the function and save the return value of the function result = add(3,5) #Because result has saved the return value of add, it can be used next print(result)
6. Consider whether Python can return multiple values?
Yes, not for most languages, such as Java and C + +, but Python can
Code example:
def add(a,b): c = a + b d = a*b return c,d c,d = add(3, 5) print(a,b)
7. Function usage classification
1. User defined function
i. No parameter, no return value
def Function name(): Execute statement
ii. Return value without parameter
def Function name(): Execute statement return Value to return
be careful:
- Whether a function has a return value depends on whether it has a return, because only return can return data
- There can be multiple return statements in a function, but as long as one return statement is executed, it means that the call of the function is completed
iii. with or without return value
def Function name(parameter list ): Execute statement
be careful:
- When calling a function, if you need to pass some data together, the called function needs to receive it with parameters
- The number of variables in the parameter list is determined according to the actual data transferred
iv. with parameter and return value
def Function name(): Execute statement return Value to return
2. Call function
1. Method of calling function:
Function name([Argument list])
- When calling a function with parameters, the number and order of arguments should be consistent with the requirements in the defined function. Do not change the inequality and order, otherwise the result will be wrong
- When calling a function with a return value, you can use a variable to save the calculation result of this function
7. Local and global variables
1. What is a local variable
Variables defined inside a function
def test1(): a = 200 # local variable print("---------test1--Before modification--a=%d"%a) a = 300 # Local variables can change their values print("---------test1--After modification--a=%d"%a) def test2(): a = 400 # Local variables. Local variable names can be the same for different functions without affecting each other print("---------test2--a=%d"%a) test1() test2()
Operation results:
---------test1--Before modification--a=200 ---------test1--After modification--a=300 ---------test2--a=400
Summary:
- A local variable is a variable defined inside a function
- Different functions can define local variables with the same name, but each one will not have an impact
- The role of local variables. In order to save data temporarily, variables need to be defined in the function for storage. This is its role
2. What are global variables?
If a variable can be used in both one function and other functions, such a variable is global variable
# Define a global variable a = 100 # Define two functions def test1(): print(a) def test2(): print(a)
3. The problem of homonymy between global variables and local variables
When a global variable and a local variable have the same name, how to modify the value of the global variable?
The code is as follows:
# Define a global variable a = 100 def test1(): a = 200 # local variable print("---------test1--Before modification--a=%d"%a) a = 300 # Local variables can change their values print("---------test1--After modification--a=%d"%a) def test2(): a = 400 # local variable print("---------test2--a=%d"%a) test1() test2()
Operation results:
---------test1--Before modification--a=200 ---------test1--After modification--a=300 ---------test2--a=400
Does it feel the same as the example results in local variables, which needs to be considered carefully? What is the problem with these two variables?
Modify the global variable method: use global to declare before the variable name
The code is as follows:
# Define a global variable a = 100 def test1(): global a #After the variable name a is modified by global, it becomes a global variable a = 600 # Modify the value of the global variable print("---------test1--After modifying the global variable value--a=%d"%a) a = 300 # The local variable becomes a global variable, and the value of the global variable is modified again print("---------test1--global variable--a=%d"%a) def test2(): a = 400 # Local variable invariant print("---------test2--local variable a=%d"%a) def test3(): print('---global variable---a=%d'%a) test1() test2() test3()
Operation results:
---------test1--After modifying the global variable value--a=600 ---------test1--global variable--a=300 ---------test2--a=400 ---global variable---a=300
Summary:
- Variables defined outside a function are called global variables
- Global variables can be accessed in all functions. If you modify a global variable in a function, you need to use global to declare it, otherwise an error will occur
- If the name of the global variable is the same as that of the local variable, the local variable is used. Tips * * proximity principle**
4. Scope
1. Variables defined in a function can only be used in this function (local variables)
2. Variables defined outside the function can be used in all functions (global variables)