Chapter VII basis, function 02

Chapter VII basis, function 02

First, the definition of the function

Definition: In the program, function like a feature tool, the tool is ready to define a function, encounter scenarios that make use of the calling function

Second, why use the function

To solve the following three questions:

  1. Redundancy program

  2. Bearded expansion poor

  3. Poor readability of the program

Third, how to use the function

Define, after calling

  • Defined Functions

    def 函数名(param1、param2……):
        """
        函数功能的描述信息
        :param1:描述
        :param2:描述
        :return:返回值
        """
        code 1
        code 2
        code 3
        ...
    
        return 返回值
    调用函数
    函数名(param1、param2……)
  • Function definition stage

    def func():
        bar()  # 不属于语法错误,不会报错
        print('*'*10)
  • Function call stage

    def bar():
        print('from bar')
    
    def foo():
        print('from foo')
        bar()
    
    foo()
    '''
    from foo
    from bar
    '''
    def foo():
        print('from foo')
        bar()
    
    def bar():
        print('from bar')
    
    foo()
    '''
    from foo
    from bar
    '''

Guess you like

Origin www.cnblogs.com/demiao/p/11334854.html