美文网首页
Function 函数

Function 函数

作者: 搬布 | 来源:发表于2020-03-01 11:31 被阅读0次

    自定义函数

    def say_hello():
    print("Hi, there!")
    print("Welcome to visit us.")

    注意:每个函数定义完后,记得要####空两行

    定义函数的作用

    1. 代码重用,减少重复编写相同代码
    2. 增加代码易读性,更容易阅读与维护

    函数中的参数

    参数在定义函数是编写在括号里,一个函数可带多个参数。参数的重要作用是把值传递给函数,调用函数时要注意参数的顺序不能调换。

    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")

    相关文章

      网友评论

          本文标题:Function 函数

          本文链接:https://www.haomeiwen.com/subject/zyatkhtx.html