- C++ 基礎
- C++ 首頁
- C++ 概述
- C++ 環境設定
- C++ 基本語法
- C++ 註釋
- C++ Hello World
- C++ 省略名稱空間
- C++ 常量/字面量
- C++ 關鍵字
- C++ 識別符號
- C++ 資料型別
- C++ 數值資料型別
- C++ 字元資料型別
- C++ 布林資料型別
- C++ 變數型別
- C++ 變數作用域
- C++ 多個變數
- C++ 基本輸入/輸出
- C++ 修飾符型別
- C++ 儲存類
- C++ 運算子
- C++ 數字
- C++ 列舉
- C++ 引用
- C++ 日期與時間
- C++ 控制語句
- C++ 決策
- C++ if 語句
- C++ if else 語句
- C++ 巢狀 if 語句
- C++ switch 語句
- C++ 巢狀 switch 語句
- C++ 迴圈型別
- C++ while 迴圈
- C++ for 迴圈
- C++ do while 迴圈
- C++ foreach 迴圈
- C++ 巢狀迴圈
- C++ break 語句
- C++ continue 語句
- C++ goto 語句
- C++ 建構函式
- C++ 建構函式與解構函式
- C++ 複製建構函式
- C++ 檔案處理
- C++ 檔案和流
- C++ 從檔案中讀取
C++ 中的多引數函式
C++ 多引數函式描述了函式接受多個引數的能力,或者可以稱之為具有多個引數的函式,可以使用多個輸入執行操作。此功能使函式能夠透過同時處理多個數據子集來執行更復雜的操作。
語法
在 C++ 中,您可以透過在函式的宣告和定義中列出多個引數來定義一個多引數函式,引數之間用逗號分隔。以下是基本語法:
return_type function_name(param1_type param1_name, param2_type parame2_name, ...);
其中,
**param1_type** 和 **param1_name** 分別是引數型別(例如 int、char、bool、string)及其名稱,類似地,**param2_type**(例如 int、char、bool、string)和 **param2_name** 分別是引數型別及其名稱,依此類推。
多引數函式的資料型別
將資料傳遞給多引數函式有兩種型別,如下所示:
1. 多個引數使用單一資料型別
所有引數都具有相同資料型別的函式。
示例
#include <iostream>
using namespace std;
// Function taking multiple parameters or arguments of same data type (int)
void sum(int a, int b, int c) {
cout << "Sum: " << (a + b + c) << endl;
}
int main() {
sum(1, 2, 3);
return 0;
}
輸出
Sum: 6
2. 多個引數使用多個數據型別
引數可以具有不同資料型別的函式。
示例
#include <iostream>
using namespace std;
// Function taking arguments of different data types
void getInfo(string name, int age, double height) {
cout << name << " is " << age << " years old and " << height << " meters tall." << endl;
}
int main() {
getInfo("Aman", 26, 1.78);
getInfo("Naman", 32, 1.65);
return 0;
}
輸出
Aman is 26 years old and 1.78 meters tall. Naman is 32 years old and 1.65 meters tall.
多引數傳遞技術
C++ 中的多引數(或單引數)傳遞技術是指用於將引數傳遞給函式的方法。這些技術定義瞭如何在函式內傳輸和操作資料。這裡我們將討論一些主要的技術:
1. 按值傳遞
在按值傳遞中,實際引數值的副本被傳遞給函式。對函式內部引數所做的更改不會影響原始引數。它安全且簡單,但對於大型資料結構而言,由於複製而可能效率低下。
2. 按引用傳遞
此方法傳遞對實際引數的引用,允許函式修改原始引數。函式使用原始資料而不是副本。它很有效率,因為它避免了複製,但是需要仔細處理以避免意外修改。
3. 可變型別與不可變型別
| 可變型別 | 不可變型別 |
|---|---|
| 這些型別的**例項在建立後可以修改**。 | 這些型別的**例項在建立後不能更改**。 |
| 列表、字典和集合是常見的可變型別。 | 整數、字串和元組是常見的不可變型別。 |
多引數函式的型別
在 C++ 中,函式能夠接受多個引數,這些引數可以以多種方式進行分類。以下是 C++ 中不同型別多引數函式的細分:
1. 固定數量的引數
具有固定數量的引數的函式,其中輸入引數的數量是特定且不變的。
示例
#include <iostream>
using namespace std;
// Function with a fixed number of arguments
void getDetails(int age, double height, const string& name) {
cout << "Name: " << name << ", Age: " << age << ", Height: " << height << endl;
}
int main() {
getDetails(25, 5.6, "Sam");
return 0;
}
輸出
Name: Sam, Age: 25, Height: 5.6
2. 可變數量的引數
可以接受可變數量引數或自變數的函式。這通常使用可變引數函式或模板引數包來實現。
示例
#include <iostream>
using namespace std;
// Variadic template function
template<typename... Args>
void printNumbers(Args... args) {
(cout << ... << args) << endl;
}
int main() {
printNumbers(1, 2, 3);
// Outputs: 123
printNumbers(4, 5, 6, 7, 8);
// Outputs: 45678
return 0;
}
輸出
123 45678
3. 預設引數
預設引數是具有預設值的引數,在呼叫函式時可以省略。如果未為這些引數提供引數,則函式使用預設值。
示例
#include <iostream>
using namespace std;
// Function with default parameters
void greet(const string& name = "Guest", int age = 0) {
cout << "Hello, " << name;
if (age > 0) {
cout << ". You are " << age << " years old.";
}
cout << endl;
}
int main() {
greet(); // Outputs: Hello, Guest
greet("Alice"); // Outputs: Hello, Alice
greet("Bob", 30); // Outputs: Hello, Bob. You are 30 years old.
return 0;
}
輸出
Hello, Guest Hello, Alice Hello, Bob. You are 30 years old.
4. 命名引數(C++ 特定)
儘管 C++ 不直接支援命名引數,但您可以使用類或結構體來封裝引數以實現類似的功能。
示例
#include <iostream>
using namespace std;
// Structure to encapsulate parameters
struct Person {
string name;
int age;
double height;
};
// Function that takes a parameter object
void printPerson(const Person& p) {
cout << "Name: " << p.name << ", Age: " << p.age << ", Height: " << p.height << endl;
}
int main() {
Person alice = {"Alice", 25, 5.9};
printPerson(alice);
return 0;
}
輸出
Name: Alice, Age: 25, Height: 5.9
5. 引數物件
單個引數是一個複雜型別,例如類或結構體,封裝多個相關值。
示例
#include <iostream>
using namespace std;
// Class to hold multiple parameters
class Rectangle {
public:
int width;
int height;
Rectangle(int w, int h) : width(w), height(h) {}
};
// Function that takes a parameter object
void calculateArea(const Rectangle& rect) {
cout << "Area: " << (rect.width * rect.height) << endl;
}
int main() {
Rectangle rect(10, 5);
calculateArea(rect); // Outputs: Area: 50
return 0;
}
輸出
Area: 50