Application calls and function parameters Chapter 23. Functions

Application calls and function parameters Chapter 23. Functions

A. The calling function

1. What is the function call

  • 1. In the event of application scenarios will function used to use is to call the function
  • 2. Call function, the function body executes the code until it has finished or return to perform the function body ends all code
  • 3. function has finished running all the code, if the function body do not write return, it will return None

2. Why do you need to call function

  • The use of performance function

3, three types of function calls

def func(x,y)
    代码块

#1.直接调用
func(1,2)

#2.用变量接收返回值
result = func(1,2)

#3.函数内调用函数
func1(func(1,3))

II. Application of function parameters

1, and parameter arguments

1 Katachisan

  • Parameters defined in parentheses function definition stage, called the formal parameter, the parameter is, in essence, is the variable name
  • Only a function parameter description, there is no practical significance
def func(x):
    code
# x就是形参

2 arguments

  • Passed in the function call stage parentheses parameter called actual parameters, referred to as the argument, in essence, is the value of the variable
  • Argument has practical significance
def func(x):
    code
    
func(2)  # 2就是实参

2, position parameter

1 position parameter

  • A function definition stage, according to the parameter defined sequentially from left to right, called the position parameter
  • 2 features: according to the position defined parameter, the value must be transferred, no more and no less
def func(x,y,z):
    code
# x,y,z就是位置形参

Argument 2 position

  • Function call stage 1, in accordance with the order from left to right argument definition, argument called location
  • Features 2: Reference values ​​are sequentially transmitted according to the position corresponding to the shape
def func(x,y,z):
    code
    
func(1,2,3)  # 1,2,3就是位置实参

3, keyword arguments

  • When calling a function, in the form of key = value passed as a parameter to the specified value, referred to as keyword arguments
def func(x,y):
    代码块
    
func(y=1,x=2)    # y = 2, x = 1 这种写法就是关键字实参
  • 2 Features: You can break the limit position, but still for the specified parameter assignment

  • Note 3:

    • 1 can mix position arguments and keyword arguments, but the location argument must be left in the keyword arguments
    • 2 can mix and keyword location argument arguments, but can not be repeated to assign a parameter
    def func(x,y):
        代码块
    
    func(1, y=2)
    func(y=2, 1)  # SyntaxError: positional argument follows keyword argument,位置参数必须在前
    func(1, x=1)  # SyntaxError: invalid syntax,无效语法,不能给位置形参重复赋值

4, the default parameter

  • 1 in the definition phase has already been assigned
def func(x,y=1):
    代码块
    
func(2)
  • 2 specific: the definition phase has already been assigned, which means you can not assign a value when calling

  • Note 3:

    • 1 position parameter in the default parameter must be left
    • 2. The default value of the parameter assigned only once in the definition phase, that is to say the value of the default parameter in the function definition stage may already fixed
    • 3. The default value of the parameter should normally be immutable
    m = 6
    def func(x=m):
        print(x)
    
    m = 666
    func()   # 6
    func(888) # 888 位置实参也能覆盖默认形参
    func(x=666)  # 666 关键字实参可以覆盖默认形参
    # 默认形参也可以用可变类型,但是尽量不要这样用
    def f(y={'name':'king'}):
        print(y)
    
    f()  # {'name': 'king'}

5, summary

  • 1. The arguments of the application depends on personal habits
  • 2. Application parameter:
    • 1. Most calls are not the same value, the change is large, it should make the parameter a position parameter
    • 2. Like most call value is small change, it should make the parameter to default parameter

Guess you like

Origin www.cnblogs.com/itboy-newking/p/10953482.html