# 没有参数和返回的函数
def say_hi():
print(" hi!")
say_hi()
say_hi()
# 有参数,无返回值
def print_sum_two(a, b):
c = a + b
print(c)
print_sum_two(3, 6)
def hello_some(str):
print("hello " + str + "!")
hello_some("China")
hello_some("Python")
# 有参数,有返回值
def repeat_str(str, times):
repeated_strs = str * times
return repeated_strs
repeated_strings = repeat_str("Happy Birthday!", 4)
print(repeated_strings)
# 全局变量与局部 变量
x = 60
def foo(x):
print("x is: " + str(x))
x = 3
print("change local x to " + str(x))
foo(x)
print(‘x is still‘, str(x))
输出:
hi! hi! 9 hello China! hello Python! Happy Birthday!Happy Birthday!Happy Birthday!Happy Birthday! x is: 60 change local x to 3 x is still 60