Python 函數聲明和調用 - Python零基礎入門教程


目錄

零基礎 Python 學習路線推薦 : Python 學習目錄 >> Python 基礎入門

一.前言

Python 函數是指代碼片段,可以重復調用,比如我們前面文章接觸到的 type / id 等等都是函數,這些函數是 Python 內置函數,Python 底層封裝后用於實現某些功能,對於所有的 Python 內置函數,我們只管調用即可,不需要關心具體內部如何實現。

二.Python 函數定義

Python 中,定義一個函數要使用 **def** 語句,依次寫出函數名、括號、括號中的參數和冒號:,然后在縮進塊中編寫函數體,函數的返回值用 return 語句返回;如果沒有 return 語句,默認返回 None

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python 函數使用.py
@Time:2021/3/29 08:00
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""

def functionname( parameters ):
   "函數說明"
   function_suite
   return [expression]


'''
例如:定義一個函數輸出’hello world’
'''
def cusom_print():
    print("hello world")

三.Python 函數的調用

當在 py 文件中,代碼一行一行執行,如果遇到函數的定義,編譯器會自動跳過,執行函數之后的代碼,如果想調用函數直接調用即可。

注意:函數在調用之前必須先聲明。Python 中的內置函數如:print / type 函數等等已經在 Python 編譯器內部聲明並且定義好了,我們只管調用即可,不需要關心具體內部如何實現。示例代碼如下:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python 函數使用.py
@Time:2021/3/29 08:00
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""

def custom_print():
    print("hello world")
    print("hello world")
    print("hello world")

custom_print()

'''
輸出結果:
hello world
hello world
hello world

'''

代碼分析:代碼執行到第 15 行時,編譯器發現這是一個函數聲明,編譯器並不會執行,會自動跳到函數末尾第 20 行,編譯器發現 20 行是在調用 custom_print 函數,會直接進入 custom_print 函數執行函數內的代碼第 16 / 17 / 18 行直到函數結束,這就是整個運行過程。

四.Python 函數傳參

函數可以通過外部傳遞參數,參數分為形參和實參:

def cusom_print(x): //x 是函數的形參 - 用於函數聲明
    print("cusom_print : x={}".format(x))

cusom_print(1)      //1 是函數的實參 - 用於調用函數時參數傳遞

1.Python 函數常規參數

常規而言,函數默認有幾個形參,在外部調用時就需要傳遞多少個實參,示例代碼如下:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python 函數使用.py
@Time:2021/3/29 08:00
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""

def cusom_print1(x):
    print("cusom_print1 : x={}".format(x))

def cusom_print2(x,y):
    print("cusom_print2 : x={}".format(x))
    print("cusom_print2 : y={}".format(y))

def cusom_print3(x,y,z):
    print("cusom_print3 : x={}".format(x))
    print("cusom_print3 : y={}".format(y))
    print("cusom_print3 : z={}".format(z))


cusom_print1(1)
cusom_print2(1,2)
cusom_print3(1,2,3)

'''
輸出結果:
cusom_print1 : x=1
cusom_print2 : x=1
cusom_print2 : y=2
cusom_print3 : x=1
cusom_print3 : y=2
cusom_print3 : z=3
'''

2.Python 函數缺省參數

在 Python 函數參數中,除了常規參數還有缺省參數,即缺省參數有一個默認值,

  • 如果外部調用該函數沒有給缺省參數傳遞參數,該形參直接取默認參數值;
  • ** 如果外部調用時給缺省參數傳遞了參數,那么該形參的值應該等於外部傳遞的參數;**

帶有缺省參數的函數也被稱為缺省函數,示例代碼如下:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿說編程
@Blog(個人博客地址): www.codersrc.com
@File:Python 函數使用.py
@Time:2021/3/29 08:00
@Motto:不積跬步無以至千里,不積小流無以成江海,程序人生的精彩需要堅持不懈地積累!

"""

def cusom_print4(x,y=2,z=3): # x=2,z=3 缺省參數
    print("cusom_print4 : x={}".format(x))
    print("cusom_print4 : y={}".format(y))
    print("cusom_print4 : z={}".format(z))
    print("***"*20)

cusom_print4(1)
cusom_print4(1,4)
cusom_print4(1,4,3)

'''
輸出結果:
cusom_print4 : x=1
cusom_print4 : y=2
cusom_print4 : z=3
************************************************************
cusom_print4 : x=1
cusom_print4 : y=4
cusom_print4 : z=3
************************************************************
cusom_print4 : x=1
cusom_print4 : y=4
cusom_print4 : z=3
************************************************************

'''

注意:

  • 1.缺省參數都有一個默認值,如果外部沒有給缺省參數傳遞參數,那么直接取默認值;否則等於外部傳遞的參數值

  • 2.缺省參數必須寫在函數形參的末尾

    錯誤寫法

    def cusom_print4(x,y=2,z):
    print(“cusom_print4 : x={}”.format(x))

3.Python 函數不定長參數

除了上面兩者,在函數的參數中還有一種不定長參數,即:函數的形參長度/類型都不固定,可能聽着有點蒙,這個問題我們留到下一篇文章 Python 函數不定長參數 *argc , **kargcs 講解,暫時不做過多解釋。

五.Python 函數返回值 return

函數的返回值可有可無,根據自己的使用需求而定。如果函數沒有 return 返回值,默認會返回 None(即空值),與 False 不同,它不表示 0,也不表示空字符串,而表示沒有值,也就是空值。

六.Python 函數重點總結

  • 1.函數的聲明必須在調用之前,否則會報錯;
  • 2.注意缺省參數的參數寫法;
  • 3.函數沒有使用 return,默認返回 None ;

七.猜你喜歡

  1. Python 配置環境
  2. Python 變量
  3. Python 運算符
  4. Python 條件判斷 if/else
  5. Python while 循環
  6. Python break
  7. Python continue
  8. Python for 循環
  9. Python 字符串
  10. Python 列表 list
  11. Python 元組 tuple
  12. Python 字典 dict
  13. Python 條件推導式
  14. Python 列表推導式
  15. Python 字典推導式

未經允許不得轉載:猿說編程 » Python 函數聲明和調用

本文由博客 - 猿說編程 猿說編程 發布!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM