defcheese_and_crackers(cheese_count,boxes_of_crackers):print"You have %d cheeses!"%cheese_countprint"You have %d boxes of crackers !"%boxes_of_crackersprint"Man that's enough for a party!"print"Get a blanket.\n"print"we can just gibe the function numbers directly:"cheese_and_crackers(20,30)print"OR, we can use variables from our script:"amount_of_cheese=10amount_of_crackers=10cheese_and_crackers(amount_of_cheese,amount_of_crackers)print"we can do math inside too:"cheese_and_crackers(10+20,5+6)print"And we can combine the two, variables and math:"cheese_and_crackers(amount_of_cheese+100,amount_of_crackers)
代码运行结果:
小结
在 C 语言里面,关于函数,提到过“形参”和 “实参”。Python 语言也是类似的规矩。
定义 Function 使用的变量名与运行 Function 使用的变量名可以不同。Python另一个灵活之处,是可以在运算表达式的结果当作参数传递给Function。