# 在 Python 中退出函式

Sahil Bhosale 2023年1月30日

1. 當我們要在函式退出或執行後從其返回值時。我們稍後將在程式中使用該值。
``````def add(a, b):
return a + b

print(value)
``````

``````3
``````

1. 當我們想在給定時刻停止執行函式時。
``````def add(a, b):

if a == 0:
return
elif b == 0:
return
else:
sum = a + b
return sum

print(value)
``````

``````None
``````

## Python 中的隱式返回型別

``````def solution():
name = "john"

if name == "john":
print("My name ", name)

solution()
``````

``````My name john
``````

`solution()` 函式不帶引數。在其中，我們有一個名為 `name` 的變數，然後使用 `if` 語句檢查其值是否與字串 `john` 相匹配。如果匹配，則列印 `name` 變數的值，然後退出該函式。否則，如果字串不匹配，我們將不執行任何操作而直接退出它。

## Python 中的顯式返回型別

``````def Fibonacci(n):

if n < 0:
print("Fibo of negative num does not exist")
elif n == 0:
return 0
elif n == 1 or n == 2:
return 1
else:
return Fibonacci(n - 1) + Fibonacci(n - 2)

print(Fibonacci(0))
``````

``````0
``````

Sahil is a full-stack developer who loves to build software. He likes to share his knowledge by writing technical articles and helping clients by working with them as freelance software engineer and technical writer on Upwork.