What is a function?
在程序中,函数就具备某一功能的工具事先将工具准备好即函数的定义遇到应用场景拿来就用即函数的调用所以务必记住:#函数的使用必须遵循先定义,后调用的原则拿来加()就能用
- Benefits of using functions
Reduce Code Redundancy
Enhance the extensibility of the program
The structure and readability functions of the enhanced program are divided into 2 types: 1 no parameter function
2 parameter function What is the return value?
返回值是一个函数的处理结果,如果我们需要在程序中拿到函数的处理结果做进一步的处理,则需要函数必须有返回值
The return value of the function is defined with return
The format is:
return value
注意: 1、return是一个函数结束的标志,函数内可以有多个return, 但只要执行一次,整个函数就会结束运行 2、return 的返回值无类型限制,即可以是任意数据类型 3、return 的返回值无个数限制,即可以用逗号分隔开多个任意类型的值 0个:返回None,ps:不写return默认会在函数的最后一行添加return None 1个:返回的值就是该值本身 多个:返回值是元组
Three ways to call a function
def max2 (x, y):
If x > Y:
return x
Else
Return y
Form one:
Max2 (ON)
Form two:
RES=MAX2 (3000,2000) * 12
Print (RES)
Form three:
Res=max2 (Max2 (1000,2000), 3000)
Print (RES)
Python function (not yet continued)