042 defined function calls, parameters, return values

A function definition

1. What is the function

In fact, the function is like a tool when I need to use it at any time, so they took over with.

2. How function

  • First Defined Functions
  • In calling the function

3. How to define a function

  • Defined function when the body does not perform the function of the code, just check the syntax error
'''
def 函数名():  # 定义阶段(造车轮阶段)
    """函数注释写在这里"""  # 函数相当于工具, 注释相当于工具的说明书
    <代码块>

# 使用  # 调用阶段
函数名()
'''

Two, three definitions embodiment functions

  • No-argument function
  • There are function parameters
  • Empty function
  • Parameter value is actually used to receive the external transfer function into the body, in fact, a variable name

1. The no-argument function

  • When no parameters defined function, call this function on behalf of our time does not need to pass parameters.

  • This also represents the tool can be used to directly use

  • If the body of the function code is no external logic to the incoming value, then it must be defined without reference function

    def func():
      x = input('num1':)
        y = input('num2:')
    
        print(int(x)+int(y))
    func()

2. Reference function

  • When the function is defined with reference behalf when we call this function is necessarily need to pass from the outside of the parameters of the function body

  • Although I have a reference function that is defined in this tool, but I need help with something else to be able to use

  • If the body of the function code passed in the need for external logic value, it must have a parameter defined as a function

    def add(x, y):
        return int(x) + int(y)
    
    num1 = input("num1:")
    num2 = input('num2:')
    res = add(num1,num2)
    print(res)

3. Empty function

  • When we know what the function of my function is, but do not know how, when he realized, we could start with an empty function

    def fun():
      pass

Third, call the function

  • Call the function is actually to use of this function
  • After writing the function body code directly write the function name in parentheses is the realization of a function call we said, the implementation body of the function code, all code until it hits the end of the return or complete execution of the function body
  • Function has finished running all the code, if the function body do not write return, it will return None.

1. Call function the way

  • After the function body finished, the last non-parametric function is a function name + parentheses. There is a function name + function parameters in parentheses, brackets incoming parameters
  • There may be a variable reference function to receive this call, and print variable values
  • There is a reference function can be called multiple times within a program

Fourth, the return value of the function

  • The return value is the result of an internal function code obtained through the run
  • In the function we put inside after running the code, our results are generally used to return to return, not to what I used to write the code as a way to print the results printed out.
  • If you print after print method used in the body of the function, we finally returns the result value, the function will eventually return to a default None
  • return function is a sign of the end, there can be multiple return within a function, to return as long as the function is executed, the function will end
  • The return value can return any data type returned
  • When the return value may be a plurality of return can return multiple values ​​separated by commas, brackets without returning multiple values, a default return ancestral form

Fifth, the parameters of the function

1. formal and actual parameters

1. Katachisan

  • Parameters defined in parentheses function definition stage, called the formal parameters, parameter referred to, is essentially the variable name
def fun(x,y)----x,y就是形参

2. arguments

  • Passed in the function call stage parentheses parameters, known as actual parameters, referred to the argument, in essence, is the value of the variable.
fun(1,2)

2. position parameters

1. Location parameter

  • In the definition phase function according to the parameter defined sequentially from left to right, we called the position parameter.

    def fun(x,y)---x,y就是位置形参

2. The position argument

  • Calling the function stage according to the arguments left to right sequentially defined positions called argument

    fun(1,2)

3. Note:

  • There are several locations on the parameter corresponding to the argument has several positions, corresponding to the order from left to right sense, when invoked sequentially pass value h

3. The default parameter

  • During the definition phase has already been assigned, which means you can not assign a value when calling.

  • You do not need to pass parameters, you can use the default values; use the value you pass parameters to pass.

  • The default parameter must be placed behind the position parameter

  • The default parameter values ​​assigned only once in the definition phase, which means that the default value of the parameter in the function definition phase has been fixed.

    def shopping(name='nick'):  # 形参经常会引用同一个值
        goods_dict = {1: '特斯拉', 2: '奔驰', 3: 'nick'}
        print(f'恭喜{name},得到{goods_dict[1]}一个')
    
    
    shopping('yongjiu')

4. Keyword argument

  • According to a particular value of the parameter name given keyword must precede the predetermined position to break the argument may have to parameter values ​​of one-pass, the position of argument

    def shopping(x, name='nick'):
        goods_dict = {1: '特斯拉', 2: '奔驰', 3: 'nick'}
        print(f'恭喜{name},得到{goods_dict[x]}一个')
    
    
    shopping(1, name='yongjiu')

Guess you like

Origin www.cnblogs.com/xichenHome/p/11323362.html