Getting the python function

1. What is the function

  • Function is equivalent tool has a function of
  • Use function must follow one principle: to define, after calling

2. Why do you use function

  • Organizational structure is not clear, poor readability
  • Code redundancy
  • Maintainability, scalability poor

3, how to use the function

1, define (three definitions embodiment)

# 最初框架式定义
def 函数名(参数1,参数2,...):
    """文档描述"""
    函数体
    return 值

 Given previously stressed:

  1, the application memory space saving function body
  2, the memory address binding function name above
  3, defined function code is not executed function body, but the body of the function detects Syntax

 1, invisible parameters

def func():
    print('哈哈哈')
    print('哈哈哈')
    print('哈哈哈')
# 无形参数应用场景    
def interactive():
    name=input('username>>: ')
    age=input('age>>: ')
    gender=input('gender>>: ')
    msg='名字:{} 年龄:{} 性别'.format(name,age,gender)
    print(msg)

interactive()
interactive()
interactive()
interactive()

 2, physical parameters

def func(x,y): # x=1  y=2
    print(x,y)
func(1,2)

# 有参函数的应用场景
def add(x,y): # 参数--->原材料
    # x=20
    # y=30
    res=x + y
    # print(res)
    return res # 返回值--->产品

res=add(20,30)
print(res)

 3, empty function

def func(x, y):
    pass

# 空函数的应用场景,先写框架再补
def auth_user():
    """user authentication function"""
    pass

def download_file():
    """download file function"""
    pass

def upload_file():
    """upload file function"""
    pass

def ls():
    """list contents function"""
    pass

def cd():
    """change directory"""
    pass

 Example 1:

def bar(): # bar=函数的内存地址
    print('from bar')

def foo():
    # print(bar)  输出的是bar的函数地址
    bar()
    print('from foo')

foo()

 Example 2:

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

def bar():  # bar=函数的内存地址
    print('from bar')

foo()  # 定义函数的时候不会运行,只有调用时候才会用,然而调用的时候bar函数已经放入内存当中

 Example 3:

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

foo()  # 这样的话才会报错,因为在运行的时候才会看本行代码上面的函数有没有定义

def bar():  # bar=函数的内存地址
    print('from bar')

2, call (three kinds of call mode)

 1, in the form of statements: just call the function in parentheses

interactive()
add(1,2)

 2 expression in the form:

def add(x,y): # 参数--->原材料
    res=x + y
    return res # 返回值--->产品
  • Assignment expression
res=add(1,2)
print(res)
  • Mathematical expression
res=add(1,2)*10
print(res)

 3, the function call can be used as parameters

res=add(add(1,2),10)
print(res)

 What happened function call

  1. Locate the memory address of the function by function name (print (func))

  2, the slogan is then added to trigger the execution of the function body (FUNC ())

3, the return value (in the form of three kinds of return value)

  return is a function of the end of the flag, i.e., once the code is running function body to return immediately terminates the function, and will return a value obtained as a result of this operation is returned.

 1, returns None: no return function in vivo \ return None

 2, a return value: return value

def func():
    return 10

res=func()
print(res)

 3, a plurality of return values: a plurality of values ​​separated by commas, can be returned to the return tuple

def func():
    return 10, 'aa', [1, 2]

res = func()
print(res, type(res))

Guess you like

Origin www.cnblogs.com/Lance-WJ/p/12511261.html