‘’’
函数:是代码的一种组织形式,完成一项特定的工作,一般一个函数完成一项工作
函数不要太大,可以函数套函数,可以有方法也可以没有返回
使用函数:1.先定义 2.使用函数俗称调用.
def 关键字 ,后面跟一个空格
函数名自己定义,后面的括号和冒号不可以省略
调用时,直接用函数名字就好
‘’’
def fun():
print("你正在调用函数fun")
fun()
‘’’
函数的参数以及返回值
参数:负责给函数传递一血必要的数据或者信息
1.形参:形式参数,在函数定义是否用到的参数没有具体值,只是一个占位号
2.实参:在调用的时候输入的值
3.返回值:调用函数时候执行的结果
-使用return返回结果
如果没有任何需要返回的 推荐使用return None表示函数结束
函数一旦返回return,则函数默认返回None
‘’’
def hello(person):
print("{0},你好吗?".format(person))
print("{},你看见我家猪了吗".format(person))
p="小明"
pp=hello(p)
print(pp)
‘’’
对于不知道的help可以为您提供帮助
help(关键字)
‘’’
help(print)
#九九乘法表
#version 1.0
for wide in range(1,10):
for high in range(1,wide+1):
print(wide*high,end=" ") # 默认有换行
print()
#尝试用函数打印出来
def haha():
for wide in range(1, 10):
for high in range(1, wide + 1):
print(wide * high, end=" ") # 默认有换行
print()
return None
haha()
#利用两个函数打印乘法表
#widey打印每一行
def widey(width):
for i in range(1,width+1):
print(i*width,end=" ")
def jiujiu():
for wide in range(1, 10):
widey(wide)
print()
‘’’
参数详解
1.普通参数
2.默认参数
3.关键字参数
4.搜集参数
‘’’
#1.普通参数,记住有几个写几个
def noral(one ,two):
print(one+two)
return None
noral(1,2)
#2. 默认参数,你不给我就默认,给我就用你的那个
def abnormal(one,tow,three=1000):
print(one+tow)
print(three)
return None
abnormal(100,200)
#关键字参数,函数参数不依赖传输的顺序
def cdnormal(one,two ,three):
print(one+two)
print(three)
cdnormal(one=20,three=3,two=4)