The basic function of using Python--

Use basic functions

1. What is the function

  • 1.1 function equivalent tool has a function of
  • 1.2 Use function must follow a principle: to define, after calling

2. Why use function

  • Organizational Structure 2.1 program is not clear, poor readability
  • 2.2 code redundancy
  • 2.3 maintainability, poor scalability

3. How function

  • 3.1 define

    '''
    def 函数名(参数1,参数2,...):
        """文档描述"""
        函数体
        return 值
    '''
    # 三种定义方式
  • 3.2 After calling

    # 三种调用方式
  • 3.3 Return value

    # 三种返回值的形式

4. Call the function return value of the function

  • In the form of a: no function parameter

    '''
    def func():
        # x # 定义函数阶段不会报错,只有当调用时才会报错
        print("哈哈哈")
    func()
    '''
    
    # 定义函数发生的事情
    '''
    1、申请内存空间保存函数体代码
    2、将上述内存的地址绑定函数名
    3、定义函数不会执行函数体代码,但是会检测函数体语法
    '''
    
    # 调用函数发生的事情
    '''
    1、通过函数名找到函数的内存地址
    2、然后加括号就是在触发函数体代码的执行
    print(func)
    func()
    '''
    
    # 示范1
    def bar():
        print('from bar')
    def foo():
        # print(bar)
        bar()
        print('from foo')
    foo()
    
    # 示范2
    def foo():
        # print(bar)
        bar()
        print('from foo')
    def bar():
        print('from bar')
    foo()
    
    # 示范3
    def foo():
        # print(bar)
        bar()
        print('from foo')
    foo()
    def bar():
        print('from bar')
    
    # 无参函数的应用场景
    def interactive():
        name = input('username>>:')
        age = input('age>>:')
        msg = '名字:{} 年龄:{}'.format(name, age)
        print(msg)
    interactive()
  • Two forms: with a function parameter

    def func(x, y):
        print(x, y)
    func(1, 2)
    # 有参函数的应用场景:
    def add(x, y): # 参数=>原材料
        res = x + y
        # print(res)
        return res # 返回值=>产品
    add(10, 2)
    res = add(10, 20)
    print(res)
  • In the form of three: empty function

    def func(x, y):
        pass
    
    func()
    
    # 空函数的应用场景
    # 构思代码时
    def login():
        pass
    def register():
        pass
  • call function

    # 1、语句的形式:只加括号调用函数
    def foo():
        print('from foo')
    foo()
    # 2、表达式形式
    def add(x, y): # 参数=>原材料
        res = x + y
        return res # 返回值=>产品
    # 2.1、赋值表达式
    res = add(10, 20)
    print(res)
    # 2.2、数学表达式
    res = add(10, 20)*3
    print(res)
    # 3、函数调用可以当作参数
    res = add(add(1, 2), 10)
    print(res)
  • Function's return value

    # 1、return: return是函数结束的标志
    # 即函数体代码一但运行到return会立刻终止函数的运行,并且会将return后的值当作本次运行的返回值返回
    def foo():
        print('111')
        return
      print('222')
        print('222')
    foo()
    
    # 2、返回一个值:return 值
    def func():
        return 10
    res = func()
    print(res)
    
    # 3、返回多个值:用逗号分隔开多个值,会被return返回成元组
    def func():
        return 10, 'aa', [1, 2]
    res = func()
    print(res, type(res))
    
    # 4、返回None,函数体内没有return
    #                     return
    #                     return None

Guess you like

Origin www.cnblogs.com/guanxiying/p/12509865.html