python3 基礎知識 六 函式 上

2021-08-25 08:26:43 字數 4166 閱讀 9863

函式能提高應用的模組性,和**的重複利用率。你已經知道python提供了許多內建函式,比如print()。但你也可以自己建立函式,這被叫做使用者自定義函式。

定義函式

定義函式的格式如下:

def 函式名():

**

demo:
# 定義乙個函式,能夠完成列印資訊的功能

def printinfo():

print('------------------------------------')

print (' 人生苦短,我用python')

print('------------------------------------')

呼叫函式

定義了函式之後,就相當於有了乙個具有某些功能的**,想要讓這些**能夠執行,需要呼叫它

呼叫函式很簡單的,通過 函式名() 即可完成呼叫

demo:

# 定義完函式後,函式是不會自動執行的,需要呼叫它才可以

定義函式的較詳細解釋

函式的文件說明

>>> def test(a,b):

... "用來完成對2個數求和"

... print("%d"%(a+b))

...

>>>

>>> test(11,22)

33

如果執行,以下**

```python

help(test)

能夠看到test函式的相關說明

help on function test in modulemain:

test(a, b)

用來完成對2個數求和

(end)

5. ####函式引數

>以下是呼叫函式時可使用的正式引數型別:

- 必需引數(位置引數)

- 關鍵字引數

- 預設引數

- 不定長引數

---- ####必需引數(位置引數)

必需引數須以正確的順序傳入函式。呼叫時的數量必須和宣告時的一樣。

呼叫printme()函式,你必須傳入乙個引數,不然會出現語法錯誤:

demo:

```python

#!/usr/bin/python3

#可寫函式說明

def printme( str ):

"列印任何傳入的字串"

print (str)

return

呼叫printme函式

printme()
以上例項輸出結果:

traceback (most recent call last):

file "test.py", line 10, in printme()

typeerror: printme() missing 1 required positional argument: 'str'

呼叫的時候帶入引數就不會出現上面的錯誤了.!

使用關鍵字引數允許函式呼叫時引數的順序與宣告時不一致,因為 python 直譯器能夠用引數名匹配引數值。

以下例項在函式 printme() 呼叫時使用引數名:

#!/usr/bin/python3

#可寫函式說明

def printme( str ):

"列印任何傳入的字串"

print (str)

return

#呼叫printme函式

printme(str="叩丁狼教育")

以上例項輸出結果:

叩丁狼教育

以下例項中演示了函式引數的使用不需要使用指定順序:

#可寫函式說明

def printinfo( name, age ):

"列印任何傳入的字串"

print ("名字: ", name)

print ("年齡: ", age)

return

#呼叫printinfo函式

printinfo( age=18, name="wolfcode" )

以上例項輸出結果:

名字: wolfcode

年齡: 18

demo:

#可寫函式說明

def printinfo( name, age = 22 ):

"列印任何傳入的字串"

print ("名字: ", name)

print ("年齡: ", age)

return

#呼叫printinfo函式

printinfo( age=18, name="wolfcode" )

print ("------------------------")

printinfo( name="wolfcode" )

以上例項輸出結果:

名字: wolfcode

年齡: 50

------------------------

名字: wolfcode

年齡: 22

printinfo( 70, 60, 50 )

以上例項輸出結果:

輸出:70

(60, 50)

---

- ######如果在函式呼叫時沒有指定引數,它就是乙個空元組。我們也可以不向函式傳遞未命名的變數。如下例項:

demo:

```python

# 可寫函式說明

def printinfo( arg1, *vartuple ):

"列印任何傳入的引數"

print ("輸出: ")

print (arg1)

for var in vartuple:

print (var)

return

# 呼叫printinfo 函式

printinfo( 10 )

printinfo( 70, 60, 50 )

以上例項輸出結果:

輸出:10

輸出:70

6050

demo:

# 可寫函式說明

def printinfo( arg1, **vardict ):

"列印任何傳入的引數"

print ("輸出: ")

print (arg1)

print (vardict)

# 呼叫printinfo 函式

printinfo(1, a=2,b=3)

以上例項輸出結果:

輸出:

1

宣告函式時,引數中星號 * 可以單獨出現,例如:

def f(a,b,*,c):

return a+b+c

如果單獨出現星號 * 後的引數必須用關鍵字傳入。

>>> def f(a,b,*,c):

... return a+b+c

...

>>> f(1,2,3) # 報錯

traceback (most recent call last):

file "", line 1, in typeerror: f() takes 2 positional arguments but 3 were given

>>> f(1,2,c=3) # 正常

python3 基礎知識 六 函式 下

所謂 返回值 就是程式中函式完成一件事情後,最後給呼叫者的結果 如下示例 def add2num a,b c a b return c 或者def add2num a,b return a b 儲存函式的返回值示例如下 定義函式 def add2num a,b return a b 呼叫函式,順便儲...

python3 函式 基礎知識二

1.python 函式的引數傳遞 1.傳遞方式 位置傳參 序列傳參 關鍵字傳參 字典關鍵字傳參 2.位置傳參 實際呼叫引數 實參 的對應關係與形式引數 形參 的對應關係 是按位置來一次對應的 示意 def fx a,b,c pass fx 1,2,3 3.序列傳參 序列傳參是批在函式呼叫過程中,用 ...

python3 基礎知識 函式應用 上

乙個程式的所有的變數並不是在哪個位置都可以訪問的。訪問許可權決定於這個變數是在 賦值的。變數的作用域決定了在哪一部分程式你可以訪問哪個特定的變數名稱。兩種最基本的變數作用域如下 demo如下 定義全域性變數 a 100 def test1 print a def test2 print a 呼叫函式...