第3-1课 函数
函数: 实现某些特定功能的并且被命名的代码块
函数的格式
变量的作用域
#全局变量:函数外部的变量 皇帝
#避部变量:函数内部的变量 将军
a = 666
def print_n():
b=123
print(b)
print_n()
print(b)
冲突处理
就近原则:奖在外君命有所不受
a = 10
def p_n():
a = 999
print(a)
p_n()
通过global 声明改变全局变量
a = 10
def p_n():
global a
a = 999
print(a)
p_n()
print(a)
课堂练习
第3-2课 参数与返回值
有参函数,具有可变性 Dev.step(N)
无参函数,实现特定功能
turnLeft(),turnRight()
返回进一步处理 多个return
课堂练习
def fun(n):
res = 1
for i in range(1,n+1):
res = res * i
return res
a = int(input())
print(fun(a))