函數#
介紹#
函數是 C++ 程式設計的基本組成部分,它將程式碼片段打包成可重複使用、高效的程式碼段,用於執行特定任務。函數可以在程式中多次調用,從而簡化程式碼組織,並有助於避免重複程式碼。此外,函數也使程式碼更易於調試。
本頁涵蓋了 C++ 中函數使用的基礎知識,包括:
函數結構和語法#
在 C++ 中,函數必須聲明,包括傳回類型、函數名稱和參數。基本語法如下:
return_type function_name(parameters) {
// Code to execute when the function is called
return result; // Optional, used to return a value
}
功能部件 |
描述 |
|---|---|
|
函数返回的数据类型,一些常见选项包括:
|
|
函數名稱。 |
|
可選參數。參數清單以逗號分隔,格式為 |
|
Optional. The value returned by the function. If the function’s |
注意: 函數必須始終在呼叫之前宣告*,否則必須提供函數原型。
功能原型#
在 C++ 中,程式碼是從上到下讀取的。這意味著函數必須先被編譯器識別,才能被呼叫。
函數原型允許你在編寫完整程式碼之前使用該函數。這樣,你就可以將 main() 放在程式頂部附近,而將函數定義放在後面,這樣更容易組織和閱讀。
如果沒有原型,你必須定義 main()上面的每個函數,這會使專案在發展過程中更難追蹤。
定義函數原型(在
int main()之外)// The function's prototype void greeting();呼叫內部函數
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); greeting(); }在函數體之後寫出函數體。
void greeting() { Brain.Screen.print("Hello!"); }
定義和呼叫函數#
Functions with No Parameters#
如果一個函數不需要輸入,你可以定義一個不帶參數的函數。如果函數不傳回值,則傳回類型應使用 void。
定義函數(在
int main()之外)// Define a function to display a message void greeting() { Brain.Screen.print("Hello!"); }呼叫內部函數
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Call the function to display the message greeting(); }
Functions with Parameters#
你也可以向函數添加參數,從而將訊息傳遞給函數使用。
注意: 可以使用 C 風格的字串(const char*)或 C++ 字串(std::string )傳遞文字。
定義函數(在
int main()之外)// Define a function with a parameter void namedGreeting(const char* name) { Brain.Screen.print("Hello, %s!", name); }在
int main()中呼叫帶參數的函數int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Call the function to display the message namedGreeting("Stranger"); }
傳回值#
函數可以使用 return 關鍵字將資料傳回給呼叫者。這樣,您就可以捕獲輸出並在專案中使用它們。
定義函數(在
int main()之外)// Define a function that multiplies numbers by 2 int timesTwo(int number) { return number * 2; }在
int main()中呼叫帶參數的函數int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Display the return value Brain.Screen.print("%d", timesTwo(2)); }
引用傳遞#
By default, C++ passes function parameters by value, meaning the function receives a copy of the original variable. Changes made inside the function do not affect the original value.
定義函數(在
int main()之外)// Add 1 to the number void addOne(int number) { number++; }在
int main()中呼叫帶參數的函數int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); int value = 5; addOne(value); // "value" will still be 5 }
若要允許函數修改原始變數,可以使用 & 符號依引用傳遞參數。
定義函數(在
int main()之外)// Add 1 to the number void addOne(int& number) { number++; }在
int main()中呼叫帶參數的函數int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); int value = 5; addOne(value); // "value" will now be 6 }
依參考傳遞允許函數更新在函數外部定義的 variables 而不傳回值。
回呼函數#
回調函數是指被傳遞給另一個函數並在特定事件發生時自動呼叫的函數。
回調函數通常用於按鈕按下或感測器更新等事件。
當事件發生時,項目會自動為你呼叫該函數,而無需你手動呼叫。
定義回呼函數(在
int main()之外)// Create a callback function void onPressed() { Brain.Screen.print("Screen pressed!"); }在
int main()中註冊回呼函數int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Call onPressed whenever the screen is pressed Brain.Screen.pressed(onPressed); }
Callback Signatures#
每個回呼函數都必須遵循特定的簽名,定義了:
返回類型
參數數量
參數類型
所需的簽章由 API 決定。回調函數必須與此簽章完全匹配。
// This callback function cannot include any parameters
void callback(void);
// This callback function includes four parameters
void callback(axisType axis, double x, double y, double z);
參數名稱具有描述性,可以有所不同,但傳回類型、參數順序和參數類型必須完全符合。
帶有預設參數的函數#
函數可以為一個或多個參數定義預設值。當呼叫函數時未為某個參數提供值時,將使用預設參數。
預設參數規則:
預設參數必須是函數定義中的最後一個參數。
一旦某個參數有了預設值,它之後的所有參數也都必須有預設值。
如果函數在其原型中指定了預設參數,則這些預設值不能在函數定義中再次指定。
定義函數(在
int main()之外)// Required parameter first, default parameter last void greeting(const char* name, int repeatCount = 1) { for (int i = 0; i < repeatCount; i++) { Brain.Screen.print("Hello, %s!", name); Brain.Screen.newLine(); } }呼叫內部函數
int main()int main() { /* vexcodeInit() is only required when using VEXcode. Remove vexcodeInit() if compiling in VS Code. */ vexcodeInit(); // Say what name to greet (EXP) and // use default value for repeatCount (1) greeting("EXP"); Brain.Screen.newLine(); // Overrides the default value to 3 greeting("EXP", 3); }
// Required parameter first, default parameter last
void greeting(const char* name, int repeatCount = 1) {
for (int i = 0; i < repeatCount; i++) {
Brain.Screen.print("Hello, %s!", name);
Brain.Screen.newLine();
}
}
int main() {
vexcodeInit();
// Say what name to greet (EXP) and
// use default value for repeatCount (1)
greeting("EXP");
Brain.Screen.newLine();
// Overrides the default value to 3
greeting("EXP", 3);
}