Python 基礎 函式引數及呼叫

2022-09-13 15:45:16 字數 4896 閱讀 4205

函式中return返回值功能

這次是要來說明一下,return到底在函式中有什麼作用

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

def test1():

print('in the test1')

return 0

print('test end')

test()

---------------執行結果---------------

in the test1

process finished with exit code 0

上面的程式碼說明:

那如果要接收return的返回值,要怎麼接收呢?其實很簡單,直接賦予乙個變數

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

def test1():

print('in the test1')

return 0

print('test end')

x = test1()

print(x)

---------------執行結果---------------

in the test1

0process finished with exit code 0

這樣就可以接收到return返回值,那return返回值可以接收哪幾種型別?

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

def test1():

print('in the test1')

def test2():

print('in the test2')

return 0

def test3():

print('in the test3')

return 1, 'hello', ['ironman', 'tony stark'],

def test4():

print('in the test4')

return test2 # 高階函式

w = test1()

x = test2()

y = test3()

z = test4()

print('test1函式return值:', w)

print('test2函式return值:', x)

print('test3函式return值:', y)

print('test4函式return值:', z)

---------------執行結果---------------

in the test1

in the test2

in the test3

in the test4

test1函式return值: none

test2函式return值: 0

test3函式return值: (1, 'hello', ['ironman', 'tony stark'], )

test4函式return值: process finished with exit code 0

程式碼說明:

總結:為什麼要有返回值?簡單說就是取得整個函式執行完的結果,可以提供給後面的程式使用。

函式呼叫

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

def test(x, y):

print(x)

print(y)

test()

---------------執行結果---------------

traceback (most recent call last):

file "/python/project/path/function_4.py", line 8, in test()

typeerror: test() missing 2 required positional arguments: 'x' and 'y'

process finished with exit code 1

上面程式碼,如果直接呼叫就會噴error,原因是因為少了二個位置引數xandy,所以在呼叫時,定義二個值給xandy再執行一次

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

def test(x, y):

print(x)

print(y)

test(1, 2)

---------------執行結果---------------12

假設如果再多給乙個實際引數會發生什麼事呢?

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

def test(x, y):

print(x)

print(y)

test(1, 2)

---------------執行結果---------------

traceback (most recent call last):

file "/python/project/path/function_4.py", line 8, in test(1, 2, 3)

typeerror: test() takes 2 positional arguments but 3 were given

process finished with exit code

觀察上面結果是說,只有2個位置引數,但給了3個實際引數所以無法執行,即然多乙個不行,那如果少乙個呢?

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

def test(x, y):

print(x)

print(y)

test(1)

---------------執行結果---------------

traceback (most recent call last):

file "/python/project/path/function_4.py", line 8, in test(1)

typeerror: test() missing 1 required positional argument: 'y'

process finished with exit code 1

再次觀察結果是說,少了乙個y的位置引數,由此可知,位置引數跟實際引數是要一一對應,但有些情況下,我們並不做一一對應的話,又該怎麼做呢?這時候就可以使用關鍵字引數呼叫

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

def test(x, y):

print(x)

print(y)

test(1, 2)

test(y=3, x=4)

---------------執行結果---------------12

43process finished with exit code 0

程式碼說明:

那有沒有可能混用上面二種用法呢?答案是會有的

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

def test(x, y, z):

print(x)

print(y)

print(z)

test(1, y=2, 4)

---------------執行結果---------------

file "/python/project/path/function_4.py", line 9

test(1, y=2, 4)

^syntaxerror: positional argument follows keyword argument

process finished with exit code 1

觀察上面程式碼,是說語法錯誤,是因為關鍵字引數不能寫在位置引數前面,所以我們修改一下

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

def test(x, y, z):

print(x)

print(y)

print(z)

test(1, z=2, y=3)

---------------執行結果---------------13

2process finished with exit code 0

參考資料:

Python 函式的定義 引數及呼叫

定義乙個函式要使用def語句,依次寫出函式名 括號 括號中的引數和冒號,然後,在縮排塊中編寫函式體,函式的返回值用return語句返回。如果你已經把my abs 的函式定義儲存為abstest.py檔案了,那麼,可以在該檔案的當前目錄下啟動python直譯器,用from abstest import...

Python學習之路 函式引數及呼叫

return 結束函式並返回值 沒有return時 返回none 返回值數 1時 返回具體值 返回值是數字 字串 列表等 返回乙個元組 需要return是需要函式完整呼叫 def test1 print in the test1 def test2 print in the test2 return...

Python 函式及引數

函式引數定義的順序必須是 必選引數 預設引數 可變引數 命名關鍵字引數和關鍵字引數。使用遞迴函式需要注意防止棧溢位。在計算機中,函式呼叫是通過棧 stack 這種資料結構實現的,每當進入乙個函式呼叫,棧就會加一層棧幀,每當函式返回,棧就會減一層棧幀。由於棧的大小不是無限的,所以,遞迴呼叫的次數過多,...