python--函数的定义和调用

函数

 

#def 函数名(参数1,参数2):
# """文档描述"""
# 函数体
# return n
"""定义函数的三种方式"""
#形式一,无参数
# def func():
# print("hahaha")
# func()
#定义函数发生的事情
# 1,申请内存空间保存函数代码
# 2,将上述内存地址绑定函数名
# 3. 定义函数不会执行函数体代码,但是会检测函数的语法

#调用函数发生的事情
# 1,通过函数名找到函数的内存地址
# 2。然后加括号就是在出发函数体代码的执行
# func()
# def bar():
# print("from bar")
# x=111
# def foo():
# print(x)
# bar()
# print("from 00")
# foo()

"""形式函数:有参函数"""
# def func(x,y):
# print(x,y)
# func(1,2)

"""形式三:空函数"""
# def func(x,y):
# pass
# func(1,2)

"""三种定义方式用在何处"""

1.无参数应用场景
def interactive():
    name=input("please input your name:")
    age=input("please input your age:")
    msg="名字:{},年龄:{}".format(name,age)
    print(msg)
interactive()
please input your name:zhanzhan
please input your age:29
名字:zhanzhan,年龄:29

 2. 有参数应用场景:

# def add(x,y):
#     print(x+y)
# add(1,5)
def add(x,y):
    res=x + y
    return res
add(1,6)

3. 空函数应用场景:
def func():
  pass

就是构思,有一个思路

 

"""调用函数"""
"""# 1.语句的形式,只加括号调用函"""
# interactive()
# add(1,2)
#
"""2.表达式形式:"""
# def add (x,y): ##参数->原材料
# res = x + y
# return res ##返回值->产品
# res=add(1,2)
# print(res)
# add(1,2)

#add(1,2)*3

"""函数调用可以当作参数"""
# def add(x,y):
# res=x+y
# return res
# res=add(add(1,2),4)
# print(res)

"""函数返回值"""
"""
1。return是函数结束的标志,即函数体代码一旦运行到return,就会立即终止函数的运行,并将入
return后的值当作本次运行的结果返回
2。函数体内没有return,或者return后没有值,则返回的是None
3。返回一个值,return 值
4。 返回多个值,用逗号分隔开多个值,会被return返回成元组"""
# 1.
# def func(x,y):
# print("11111")
# print("22222")
# res=x+y
# return res
# res=func(1,2)
# print(res)

# def func(x,y):
# print("11111")
# res = x + y
# return res
# print("22222")
# res=func(1,2)
# print(res)

"""
11111
3
"""
def func():
    return 10,20,[1,2]
res=func()
print(res,type(res))
(10, 20, [1, 2]) <class 'tuple'>

 


"""
(10, 20, [1, 2]) <class 'tuple'>
"""

 

posted @ 2020-12-09 23:35  正霜霜儿  阅读(202)  评论(0编辑  收藏  举报