自定义函数
def say_hello():
print("Hi, there!")
print("Welcome to visit us.")
注意:每个函数定义完后,记得要####空两行
定义函数的作用
- 代码重用,减少重复编写相同代码
- 增加代码易读性,更容易阅读与维护
函数中的参数
参数在定义函数是编写在括号里,一个函数可带多个参数。参数的重要作用是把值传递给函数,调用函数时要注意参数的顺序不能调换。
def greetings(name):
print(f 'Hi {name} !')
print( 'Welcome to visit us')
另外一个方法是,传递参数时使用关键字进行,这样就不用考虑参数的顺序了。使用关键字能够增强可读性。
def greetings(first_name, last_name):
print(f "Hi {first_name} {last_name}!")
print("Welcome to visit us")greetings(last_name = "Smith", first_name="Tomy")
网友评论