1.第一种没有参数,没有返回值
def go():
print("hello fun")
for i in range(10):
go()
运行一下:
D:\untitled\venv\Scripts\python.exe D:/untitled/6.py
hello fun
hello fun
hello fun
hello fun
hello fun
hello fun
hello fun
hello fun
hello fun
hello fun
Process finished with exit code 0
定义一个函数,通过调用这个函数,打印循环打印10次
2.第二种有参数,没有返回值
def goprint(num):
print("python",num)
for i in range(10):
goprint(i)
运行一下:
D:\untitled\venv\Scripts\python.exe D:/untitled/6.py
python 0
python 1
python 2
python 3
python 4
python 5
python 6
python 7
python 8
python 9
Process finished with exit code 0
3.第三种没有参数,有返回值
def getdata():
return random.randint(0,100)
这是一个获取随机数的方法,函数中没有参数,但是又返回值,用return返回0-100之间的随机数
运行一下:
hello fun
python 0
36
hello fun
python 1
93
hello fun
python 2
3
hello fun
python 3
35
hello fun
python 4
24
hello fun
python 5
38
hello fun
python 6
20
hello fun
python 7
4
hello fun
python 8
45
hello fun
python 9
33
4.第四种既有参数,又有返回值
def add(num1,num2):
return num1+num2
res=add(100,260)
print(res)
运行一下
360
这种是既有输入又有输出,是一种最常见的函数