1,格式
def func_name ( /* some optional params */) :
2,局部变量与全局变量
全局变量
(1) global var_name
(2) 尽量在函数内定义全局变量,避免在函数外面定义;
(3) 全局变量在没被赋值之前,可以认为它不起作用.
3,默认参数
默认参数在函数声明时即赋值;应把参数的默认赋值放在所以参数声明的最后面。
例如 def func(b, a=1):
4,关键参数
通过给与参数同名的变量赋值实现
例如: def func(a, b=1, c=2) :
# something here
func(a=3, c=5)
5,return
通过return跳出函数;并且可以借return返回一个值