###### tags: `Python` # CH4 函式 ## 使用者自訂 ::: danger **要用函式前請注意有無跑過此函式** ::: 1. 基本型 ```python= def f(): print("Hello World!") f() ``` 2. 輸入型 ```python= def f(word): print("Hello "+word) a=input() f(a) ``` 3. 輸入+輸出 ```python= def f(a): return a+1 # return 會結束函式! a=int(input()) print(f(a)) ``` 4. 匿名函式 ```python= f=lamda x:x*2 print(f(int(input()))) ``` ## 遞迴 :::danger **使用遞迴時請注意!** 1. 中止條件 2. 呼叫方式 ::: ### 經典問題~階層 ![](https://i.imgur.com/WlMdDRW.png) ```python= def f(x): if x==1:return 1 else:return x*f(x-1) print(f(int(input()))) ```