1,函数关键字 def
举个例子:
>>> def MyFirstFunction():
print('第一个函数!')
>>> MyFirstFunction() #调用函数时只要输入函数名称即可
第一个函数!
2,函数的参数
举个例子:
>>> def MySecondFunction(name): #()里的内容既是参数
print(name + 'python')
>>> MySecondFunction('love,')
love,python
>>> def add1(num1, num2): #函数可以设置多个参数,但是,参数越多越容易出错
result = num1 + num2
print(result)
>>> add1(2, 2)
4
0. 编写一个函数power()模拟内建函数pow(),即power(x, y)为计算并返回x的y次幂的值。
>>> def power(x, y):
result = 1
for i in range(y):
result *= x
return result
>>> print(power(2, 3))
8
>>> print(power(3, 4))
81
>>> print(power(4, 2))
16