# 【4-1】副函式 副函式就是你自己定義的一段功能。它有名字、可以接收資料(參數),也可以把結果回傳(`return`)。 輸入一個東西 → 進行一個操作 → 再輸出一個東西(也可以不輸出) 例如:飲料罐裝的填充機器,輸入一個瓶子,進行填裝飲料,再輸出一個裝滿飲料的瓶子。 ## 基本語法 ```cpp 資料型別 副函式名稱(參數列表) { // 要執行的程式碼 return 回傳值; // 如果資料型別不是 void,就需要回傳值 } ``` 要注意,回傳值必須和資料型別相同! ### Python對照 Python 是動態型別語言,不用事先宣告變數的資料型別,也不需要在函式定義時指定回傳型別,所以接下來的 Python 範例中,都不會特別寫資料型別,也不需要 `void`、`int`、`bool`,只需要用 `def` 就好了。 ```python def 副函式名稱(參數列表): # 要執行的程式碼 return 回傳值 # 如果沒有回傳值可以省略 return 或寫 return None ``` ## 資料型別 可以去複習 [【1-1】變數宣告](https://hackmd.io/@LAfWxjSxRASps-4O_bppsA/H1PxDlSVeg) ,只多了一個 `void`,意思是不需要回傳值。 ## 副函式基本守則 1. 如果非全域變數,就必須要傳遞變數給副函式。(忘記的可以去複習 [【1-3】變數範圍](https://hackmd.io/@LAfWxjSxRASps-4O_bppsA/ryRUM4HEle)) ```cpp=1 #include <bits/stdc++.h> using namespace std; // 傳遞變數,位置一一對應,資料型態也要相同 // 整數 x 對應到整數 a ,整數 y 對應到整數 b void add(int a, int b) { // 可以在這裡進行某些操作 } int main() { int x = 5, y = 3; add(x, y); // 傳遞變數 x 和 y 到 add 函式 return 0; } ``` 2. 傳遞陣列時,直接使用陣列的名字當指標就可以了。 ```cpp=1 #include <bits/stdc++.h> using namespace std; // 一維整數陣列 nums 對應到 一維整數陣列 arr // 可以不用給定大小 void sumArray(int arr[]) { // 可以在這裡進行某些操作 } int main() { int nums[] = {1, 2, 3, 4, 5}; sumArray(nums); // 傳遞陣列到副函式 return 0; } ``` 3. 如果是二維陣列,則必須將大小先訂好至少一個。 ```cpp=1 #include <bits/stdc++.h> using namespace std; // 二維整數陣列 grid 對應二維整數陣列 arr // 需先給定一個維度的大小 void sum2DArray(int arr[][3]) { // 可以在這裡進行某些操作 } int main() { int grid[2][3] = { {1, 2, 3}, {4, 5, 6} }; sum2DArray(grid); // 傳遞二維陣列 return 0; } ``` 4. 若是希望傳遞過去的東西不要被更改,可以加上 `const` 來保護變數。 ```cpp=1 // 確保陣列操作時不會更改其內容 void Array(const int arr[]) { // 可以在這裡進行某些操作 } ``` ### Python對照 Python 沒有 `const`,如果不想讓函式改變 list,通常會在函式內部複製一份或不要做修改。 ## int `int` 代表此副函式最後會回傳一個整數,常見的使用方法為「遞迴」,我們在下一個章節[【4-2】遞迴](https://hackmd.io/@LAfWxjSxRASps-4O_bppsA/Hy8oVcQrel) 會提到。 ```cpp=1 #include <bits/stdc++.h> using namespace std; // 副函式:計算兩個數字的和,並返回結果 int sum(int a, int b) { return a + b; } int main() { int x, y; cin >> x >> y; // 調用 sum 函式,並將結果存儲在 result 中 int result = sum(x, y); cout << "The sum of " << x << " and " << y << " is " << result << endl; return 0; } ``` ### Python 對照 ```python def sum(a, b): return a + b x = int(input()) y = int(input()) result = sum(x, y) print(f"The sum of {x} and {y} is {result}") ``` ## void `void` 代表「無」,即該函式不返回任何資料或結果。通常用於僅執行某些操作(如打印訊息、修改參數等)而不需要返回結果的函式。特別注意,如果需要終止 `void` 類型的副函式的話,仍然可以使用 `return` ,只不過不需要加入回傳值(就寫一個 `return` 就好了)。 ```cpp=1 #include <bits/stdc++.h> using namespace std; // 副函式:檢查數字是否為正數,若是則打印,否則提前退出 void printPositiveNumber(int num) { if (num <= 0) { cout << "The number is not positive. Exiting function." << endl; return; // 提前結束函式執行 } cout << "The number is positive: " << num << endl; } int main() { int num; cin >> num; // 呼叫函式處理數字 printPositiveNumber(num); return 0; } ``` ### Python 對照 ```python def print_positive_number(num): if num <= 0: print("The number is not positive. Exiting function.") return print(f"The number is positive: {num}") num = int(input()) print_positive_number(num) ``` ## bool `bool` 會回傳 `true` 或者 `false`,通常用於判斷此操作是否合法,例如迷宮的邊界條件。 ```cpp=1 #include <bits/stdc++.h> using namespace std; // 副函式:檢查一個數字是否是 2 的倍數 bool isMultipleOfTwo(int n) { return n % 2 == 0; /* 可以看成下方這樣子 if(n % 2 == 0){ return true; } else{ return false; }*/ } int main() { int num; cin >> num; // 調用 isMultipleOfTwo 函式,檢查 num 是否是 2 的倍數 if (isMultipleOfTwo(num)) { cout << num << " is a multiple of 2." << endl; } else { cout << num << " is not a multiple of 2." << endl; } return 0; } ``` ### Python 對照 ```python def is_multiple_of_two(n): return n % 2 == 0 num = int(input()) if is_multiple_of_two(num): print(f"{num} is a multiple of 2.") else: print(f"{num} is not a multiple of 2.") ``` 比較常用的就是這三個資料型別,其它的依據題目需求自行運用即可,這邊不多做介紹。 --- 聯絡方式:[codecodefunny@gmail.com](mailto:codecodefunny@gmail.com) 最後編修時間:2025/07/10 子柚筆