本單元語法速查表
| 語法 / 關鍵字 | 用途 | 範例 |
|---|---|---|
回傳型 函式名(參數) |
定義函式 | int add(int x, int y) { return x + y; } |
return 值; |
回傳值並結束函式 | return x + y; |
void 函式名() |
定義不回傳值的函式 | void print() { cout << "Hi\n"; } |
函式名(參數) |
呼叫函式 | add(3, 5) |
型 &參數 |
Pass by reference | void modify(int &x) { x = 10; } |
#include<algorithm> |
引入 swap, max, min | swap(a, b) |
#include<numeric> |
引入 gcd, lcm | gcd(12, 8) |
static 型 變數 |
靜態變數(值跨函式呼叫保留) | static int count = 0; count++; |
參數 = 預設值 |
設定預設參數 | int func(int x = 5) |