函数是功能和动作的封装。
函数有参数,有返回值
当return写的时候,返回一个值,不写就返回None,只写return,也返回None。返回值可以是多个,这时接受到的就是一个元祖。
函数有两种参数
形参就是函数定义时使用的参数,也是一个变量。注意,不是实际的值。
实参就是函数调用时的参数,是实际的值。
把实际参数传递给形参的过程叫做传参。
实参有三种形式。位置参数,关键字参数,混合参数。其中关键字参数在混合时必须在关键字参数之前。
形参有三种,位置参数,默认参数,和混合参数。位置参数是按照位置传递的。默认参数保证至少有一个参数可以使用,当调用函数时,如果默认参数被传递了,就使用传递的值,否则使用定义时定义好的默认值。
混合参数比较复杂。
函数可以嵌套。