Highlights function

Highlights function

First, the definition of the function

1.1 What is the function

More function is an idea, he is not a technology

1.2 Functions defined manner

def 函数名():   #定义阶段(造车轮阶段)
    """函数注释写在这里"""    # 相当于函数的说明书
    <代码块>
函数名()  #函数的调用阶段(开车阶段)
#定义一个注册函数
def register():
    """注册函数"""
    username = input('请输入你的用户名:')
    pwd = input('请输入你的密码:')

    with open('user_info.txt', 'a', encoding='utf8') as fa:
        fa.write(f'{username}:{pwd}|')
register()

Definition phase does not execute code only detect syntax errors

Two, three way function definition

2.1 empty function (just define a function, then had nothing)

Only know the name of the tool but do not know how to select the function

def func():
    pass

Nullary 2.2 (no argument: unknown variable)

Tools can be used alone

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

2.3 Reference function

This tool can not be used alone, you have to add accessories to use

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

Third, call the function

If we enter the x, y, 4 and 6, respectively

def add(x,y)
    return x+y  #函数的返回值
    x = input('num1:')
    y = input('num2:')
res = add(x, y)     #调用add函数,并将add的返回值赋值给res
print(res*12)       #可以对函数的返回值进行其他操作
--------------------------------------------------
120    输出的最后的值为120

Fourth, the return value of the function

def add(x, y):
    # return (x, y, x + y)  # return可以返回任意数据类型
    return x, y, x + y  # return可以返回任意数据类型,不加括号返回多个值时,默认用元祖的形式返回,如果没有return返回函数值就默认返回None
    x,y = add(1, 2)
add()

return is actually a function's return value, final value and he can function, and when he returns the value of the output of the function, his following code will not run.

Fifth, the parameters of the function

5.1 Katachisan

Only parameter definition phase, in the form of parameters, nothing, knowledge of accounting positions, descriptive sense

def func(x,y)  #形参
    preturn x+y
add(1,2)    # 这里调用又是实参

5.2 arguments

Call phase only argument, the actual parameters, has a specific value, but also has real meaning

def func(x,y)  
    preturn x+y
add(1,2)  #实参

5.3 the position parameter

A parameter of a write

def add(num1,num2):   #位置形参
    '''有参函数'''
    print(int(num1) + int(num2))
add()

5.4 Location argument

A write past

def add(num1,num2):  
    '''有参函数'''
    print(int(num1) + int(num2))
add(1,2)   #位置实参

5.5 default parameter

Parameter passing need not be the default value; transmission parameter values to use your pass, the position of the finger must be placed in front of the default shape parameter

如果shopping('jiayi')里面没有传参数'jiayi'那么他就会使用默认值name='nick'
--------------------------------------------------
恭喜wenbin,得到特斯拉一个

If shopping('wenbin')there is no transmission parameters 'wenbin'then he will use the default valuesname='nick'

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

Congratulations nick, get a Tesla

5.6 Keyword argument

According to a particular value of a given parameter name, parameter can break location, must have a predetermined value of the one-pass, before argument must position the keyword arguments

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

Location argument 1must be placed Keyword argument name='wenbin', otherwise it will error, but there are several formal parameters have to pass several values

Guess you like

Origin www.cnblogs.com/wwbplus/p/11353104.html