<aside> 💡 Notion Tip: Here at Notion we use this template to help teams plan, design and develop products with the greatest chance for success. It helps teams think through their work more deeply, improves asynchronous communication with other teams, and creates space for collaboration.

</aside>

👀 如何定義函數

以def關鍵字開始,空一格命名,後面加上(),()裡面是要傳進去的參數

例子1:

def hello():
	print('Hello world!')

例子2:

def future_value(pv, rate, nper):
    fv = pv * (1 + rate)**nper
    return fv

補充: python 3.11後,自訂函式可以給type hint

https://docs.python.org/3/library/typing.html

就是在每個參數後面加上 : type

回傳值型態則是在)後面加上 -> type

如果要指定list每個元素的型別,可以這樣寫: list[type]

type可以是任何python允許的型別

例子:

def future_value(pv: float, rate: float, nper: floar) -> float:
    fv = pv * (1 + rate)**nper
    return fv

💭 位置引數 (Positional Arguments)和關鍵字引數 (Keyword Arguments)

在呼叫函數時,如果只是把變數或是值放到()裡面,則會依照自訂函數定義的參數順序,一個一個把對應的物件傳進去

⇒ 位置引數(Positional Arguments)