C++ 函数示例

功能描述

本示例程序展示了 C++ 中各种函数类型的定义和使用方法,包括:

  • 基本函数定义和调用
  • 函数参数和返回值
  • 递归函数
  • 引用参数
  • 数组参数
  • 函数重载
  • 内联函数
  • constexpr 函数
  • Lambda 表达式
  • 函数指针
  • 命名空间中的函数
  • 类成员函数
  • 默认参数
  • 可变参数
  • 函数模板

代码解析

1. 基本函数定义和调用

1
2
3
4
5
6
7
void sayHello() {
std::cout << "Hello from sayHello!" << std::endl;
}

int add(int a, int b) {
return a + b;
}

说明

  • 函数定义包括返回类型、函数名、参数列表和函数体。
  • void 表示函数没有返回值。
  • int 表示函数返回一个整数。
  • 函数调用通过函数名和参数列表完成。

2. 函数参数和返回值

1
2
3
4
5
6
7
double multiply(double x, double y) {
return x * y;
}

void printInfo(std::string name, int age) {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}

说明

  • 函数可以接受不同类型的参数,如 doublestd::stringint
  • 函数可以返回不同类型的值,如 double
  • 函数参数可以是基本类型,也可以是复合类型(如 std::string)。

3. 递归函数

1
2
3
4
5
6
int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}

说明

  • 递归函数是调用自身的函数。
  • 递归函数必须有一个终止条件(基础情况),否则会导致无限递归。
  • 这里的终止条件是 n <= 1,此时返回 1。

4. 引用参数

1
2
3
4
5
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}

说明

  • 引用参数(使用 & 符号)允许函数修改实参的值。
  • 引用参数避免了值传递的复制开销,对于大型对象尤为重要。
  • 这里的 swap 函数通过引用参数交换两个变量的值。

5. 数组参数

1
2
3
4
5
void modifyArray(int arr[], int size) {
for (int i = 0; i < size; ++i) {
arr[i] *= 2;
}
}

说明

  • 数组作为函数参数时,实际上传递的是数组首元素的指针。
  • 因此,函数内部对数组元素的修改会影响到原数组。
  • 通常需要传递数组的大小作为额外参数。

6. 函数重载

1
2
3
4
5
6
7
8
9
10
11
int sum(int a, int b) {
return a + b;
}

double sum(double a, double b) {
return a + b;
}

int sum(int a, int b, int c) {
return a + b + c;
}

说明

  • 函数重载允许定义多个同名函数,只要它们的参数列表不同(参数类型、数量或顺序不同)。
  • 编译器根据函数调用时提供的参数类型和数量来选择合适的函数版本。
  • 这里定义了三个 sum 函数:两个参数的 int 版本、两个参数的 double 版本和三个参数的 int 版本。

7. 内联函数

1
2
3
inline int square(int x) {
return x * x;
}

说明

  • inline 关键字建议编译器将函数体内联到调用点,以减少函数调用的开销。
  • 内联函数适用于简短、频繁调用的函数。
  • 编译器可以忽略 inline 关键字的建议。

8. constexpr 函数

1
2
3
constexpr int cube(int x) {
return x * x * x;
}

说明

  • constexpr 关键字表示函数可以在编译时计算其返回值。
  • constexpr 函数的参数和返回值必须是字面量类型。
  • constexpr 函数在编译时计算可以提高程序性能。

9. Lambda 表达式

1
2
auto lambda = [](int a, int b) { return a - b; };
std::cout << "Lambda: 10 - 3 = " << lambda(10, 3) << std::endl;

说明

  • Lambda 表达式是 C++11 引入的一种匿名函数语法。
  • Lambda 表达式的语法为:[捕获列表](参数列表) -> 返回类型 { 函数体 }
  • 捕获列表指定了 Lambda 表达式可以访问的外部变量。
  • auto 关键字用于自动推导 Lambda 表达式的类型。

10. 函数指针

1
2
int (*operation)(int, int) = add;
std::cout << "Using function pointer: 5 + 3 = " << operation(5, 3) << std::endl;

说明

  • 函数指针是指向函数的指针变量。
  • 函数指针的类型包括返回类型和参数类型。
  • 函数指针可以用于回调函数、函数表等场景。

11. 默认参数

1
2
3
void greet(std::string name, std::string message = "Hello") {
std::cout << message << ", " << name << "!" << std::endl;
}

说明

  • 默认参数允许函数调用时省略某些参数,使用预定义的默认值。
  • 默认参数必须从右向左定义,即如果一个参数有默认值,那么它右边的所有参数也必须有默认值。

12. 可变参数

1
2
3
4
5
6
7
8
9
10
11
12
double average(int count, ...) {
va_list args;
va_start(args, count);

double sum = 0;
for (int i = 0; i < count; ++i) {
sum += va_arg(args, double);
}

va_end(args);
return sum / count;
}

说明

  • 可变参数允许函数接受任意数量的参数。
  • 使用 <cstdarg> 头文件中的 va_listva_startva_argva_end 宏来处理可变参数。
  • 可变参数函数需要至少一个固定参数,用于确定可变参数的数量或类型。

13. 函数模板

1
2
3
4
template <typename T>
T maximum(T a, T b) {
return (a > b) ? a : b;
}

说明

  • 函数模板允许定义适用于多种类型的通用函数。
  • typename Tclass T 声明了一个类型参数。
  • 函数模板在调用时会根据实参类型自动实例化。

14. 命名空间中的函数

1
2
3
4
5
6
7
8
9
namespace Math {
int add(int a, int b) {
return a + b;
}

int subtract(int a, int b) {
return a - b;
}
}

说明

  • 命名空间用于组织代码,避免命名冲突。
  • 命名空间中的函数可以通过 命名空间::函数名 的方式调用。
  • 可以使用 using namespace 命名空间using 命名空间::函数名 来简化调用。

15. 类成员函数

1
2
3
4
5
6
7
8
9
10
class Calculator {
public:
int add(int a, int b) {
return a + b;
}

int multiply(int a, int b) {
return a * b;
}
};

说明

  • 类成员函数是定义在类内部的函数。
  • 类成员函数可以访问类的成员变量和其他成员函数。
  • 类成员函数通过对象调用,使用 对象.函数名 的语法。

编译和运行

在 Windows 上编译(使用 g++):

1
g++ -std=c++11 -fexec-charset=GBK -o functions functions.cpp

运行程序:

1
.\functions.exe

输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
=== 1. 基本函数定义和调用 ===
Hello from sayHello!
5 + 3 = 8
2.5 * 4.0 = 10

=== 2. 函数参数和返回值 ===
Name: Alice, Age: 30

=== 3. 递归函数 ===
5! = 120

=== 4. 引用参数 ===
Before swap: x = 10, y = 20
After swap: x = 20, y = 10

=== 5. 数组参数 ===
Before modify: 1 2 3 4 5
After modify: 2 4 6 8 10

=== 6. 函数重载 ===
sum(3, 4) = 7
sum(2.5, 3.5) = 6
sum(1, 2, 3) = 6

=== 7. 内联函数 ===
square(5) = 25

=== 8. constexpr 函数 ===
cube(3) = 27

=== 9. Lambda 表达式 ===
Lambda: 10 - 3 = 7

=== 10. 函数指针 ===
Using function pointer: 5 + 3 = 8
Using function pointer with operation: 10 - 4 = 6

=== 11. 默认参数 ===
Hello, Bob!
Welcome, Bob!

=== 12. 可变参数 ===
Average of 3 numbers: 3.33333
Sum of 4 numbers: 14

=== 13. 函数模板 ===
Maximum of 5 and 10: 10
Maximum of 3.14 and 2.71: 3.14
Maximum of apple and banana: banana

=== 14. 命名空间中的函数 ===
Math::add(5, 3) = 8
Math::subtract(10, 4) = 6

=== 15. 类成员函数 ===
Calculator::add(5, 3) = 8
Calculator::multiply(4, 6) = 24

=== 16. 静态函数 ===
Static function called: This is a static function
Counter: 1

=== 17. 常量成员函数 ===
Value: 42
Value after modification: 100

=== 18. 引用返回值 ===
Value before increment: 5
Value after increment: 6
Value after second increment: 7

=== 19. 移动语义和右值引用 ===
Original value: 42
Moved value: 42
Value after move: 0
Sum of two values: 84

技术要点

1. 函数声明和定义

  • 函数声明:声明函数的返回类型、函数名和参数列表,不包含函数体。
  • 函数定义:包含函数声明和函数体。
  • 函数原型:函数声明的另一种说法,用于告诉编译器函数的签名。

2. 参数传递方式

  • 值传递:函数接收参数的副本,修改参数不会影响实参。
  • 引用传递:函数接收参数的引用,修改参数会影响实参。
  • 指针传递:函数接收参数的指针,修改指针指向的值会影响实参。

3. 返回值类型

  • 基本类型:如 intdoublebool 等。
  • 复合类型:如 std::string、数组、结构体等。
  • 指针类型:返回指针时要注意避免返回局部变量的指针。
  • 引用类型:返回引用时要注意避免返回局部变量的引用。

4. 函数重载规则

  • 函数名相同。
  • 参数列表不同(参数类型、数量或顺序不同)。
  • 返回类型不同不足以构成函数重载。

5. 内联函数和 constexpr 函数

  • 内联函数:建议编译器将函数体内联到调用点,减少函数调用开销。
  • constexpr 函数:可以在编译时计算,提高程序性能。

6. Lambda 表达式

  • 捕获列表
    • []:不捕获任何变量。
    • [=]:按值捕获所有外部变量。
    • [&]:按引用捕获所有外部变量。
    • [x, &y]:按值捕获 x,按引用捕获 y。
  • 参数列表:与普通函数类似。
  • 返回类型:通常可以省略,由编译器自动推导。

7. 函数指针和回调函数

  • 函数指针:指向函数的指针变量,用于存储函数的地址。
  • 回调函数:通过函数指针传递给其他函数并在适当的时候调用的函数。

8. 默认参数和可变参数

  • 默认参数:为函数参数提供默认值,调用时可以省略这些参数。
  • 可变参数:允许函数接受任意数量的参数,使用 <cstdarg> 头文件中的宏处理。

9. 函数模板

  • 模板参数:可以是类型参数(typename Tclass T)或非类型参数(如整数)。
  • 模板实例化:编译器根据实参类型自动生成具体的函数版本。
  • 模板特化:为特定类型提供自定义的模板实现。

10. 命名空间和作用域

  • 命名空间:用于组织代码,避免命名冲突。
  • 全局作用域:在所有函数和命名空间外部定义的标识符。
  • 局部作用域:在函数、块或类内部定义的标识符。

常见问题

1. 函数未声明错误

问题:调用了一个未声明的函数。
解决方案:在调用函数之前声明函数,或在头文件中声明函数并在源文件中包含该头文件。

2. 函数参数不匹配

问题:函数调用时提供的参数类型或数量与函数声明不匹配。
解决方案:确保函数调用时提供的参数类型和数量与函数声明一致。

3. 返回类型不匹配

问题:函数返回的值类型与函数声明的返回类型不匹配。
解决方案:确保函数返回的值类型与函数声明的返回类型一致,或使用类型转换。

4. 递归栈溢出

问题:递归函数没有终止条件,或终止条件不正确,导致栈溢出。
解决方案:确保递归函数有正确的终止条件,避免无限递归。

5. 指针和引用错误

问题:使用了空指针、野指针,或返回了局部变量的引用。
解决方案:始终初始化指针,避免返回局部变量的引用,使用智能指针管理内存。

6. 函数重载歧义

问题:函数调用时,编译器无法确定应该调用哪个重载版本。
解决方案:确保函数重载的参数列表有明显的区别,或在调用时显式指定参数类型。

7. 默认参数顺序错误

问题:默认参数不是从右向左定义的。
解决方案:确保默认参数从右向左定义,即如果一个参数有默认值,那么它右边的所有参数也必须有默认值。

8. 可变参数类型错误

问题:使用 va_arg 时指定的类型与实际参数类型不匹配。
解决方案:确保 va_arg 指定的类型与实际参数类型一致。

9. 模板实例化错误

问题:模板参数不满足模板的要求,导致模板实例化失败。
解决方案:确保模板参数满足模板的要求,或为特定类型提供模板特化。

10. 命名空间冲突

问题:不同命名空间中存在同名函数,导致调用歧义。
解决方案:使用完全限定名(命名空间::函数名)来调用函数,或使用 using 声明来指定要使用的函数。

代码优化建议

  1. 函数设计原则

    • 函数应该有单一的职责,只做一件事情。
    • 函数名应该清晰地表达函数的功能。
    • 函数参数应该尽量少,避免过长的参数列表。
    • 函数体应该尽量简短,避免过长的函数。
  2. 参数传递优化

    • 对于大型对象,使用引用传递或常量引用传递,避免值传递的复制开销。
    • 对于不需要修改的参数,使用 const 引用,提高代码的可读性和安全性。
  3. 返回值优化

    • 对于大型对象,考虑使用移动语义或引用返回,避免返回值的复制开销。
    • 避免返回局部变量的引用或指针。
  4. 内联函数和 constexpr 函数

    • 对于简短、频繁调用的函数,使用 inline 关键字,减少函数调用开销。
    • 对于在编译时可以计算的函数,使用 constexpr 关键字,提高程序性能。
  5. Lambda 表达式

    • 对于简短的、只在局部使用的函数,使用 Lambda 表达式,提高代码的可读性。
    • 合理使用捕获列表,避免不必要的变量捕获。
  6. 函数模板

    • 对于需要处理多种类型的通用函数,使用函数模板,提高代码的复用性。
    • 为常见类型提供模板特化,提高特定类型的性能。
  7. 错误处理

    • 对于可能失败的函数,考虑使用异常或返回错误代码。
    • 使用 noexcept 关键字标记不会抛出异常的函数,提高编译器优化能力。
  8. 性能优化

    • 避免在热点路径上使用复杂的函数调用。
    • 考虑函数的内联可能性,特别是对于短小的函数。
    • 合理使用函数指针和虚函数,避免过度使用导致的性能损失。
  9. 代码风格

    • 遵循一致的函数命名和格式约定。
    • 为复杂函数添加注释,说明函数的功能、参数和返回值。
    • 使用空行和缩进,提高函数体的可读性。
  10. 测试和调试

    • 为函数编写单元测试,确保函数的正确性。
    • 在函数中添加适当的断言,捕获逻辑错误。
    • 使用调试器和性能分析工具,识别和解决函数中的问题。

总结

C++ 提供了丰富的函数特性,包括:

  • 基本函数:定义和调用函数,处理参数和返回值。
  • 高级特性:递归、引用参数、数组参数、函数重载。
  • 现代特性:内联函数、constexpr 函数、Lambda 表达式。
  • 高级主题:函数指针、默认参数、可变参数、函数模板。
  • 组织方式:命名空间中的函数、类成员函数。

通过合理使用这些特性,可以编写出:

  • 模块化:将复杂问题分解为简单的函数。
  • 可重用:通过函数模板和重载提高代码复用性。
  • 高效:通过内联、constexpr 和移动语义提高性能。
  • 可维护:通过清晰的函数设计和命名提高代码可读性。

函数是 C++ 程序的基本构建块,掌握函数的各种特性对于编写高质量的 C++ 代码至关重要。

functions.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <iostream>
#include <string>
#include <cstdarg>
#include <vector>

// 1. 基本函数定义
void sayHello() {
std::cout << "Hello from sayHello!" << std::endl;
}

int add(int a, int b) {
return a + b;
}

double multiply(double x, double y) {
return x * y;
}

void printInfo(std::string name, int age) {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}

// 2. 递归函数
int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}

// 3. 引用参数
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}

// 4. 数组参数
void modifyArray(int arr[], int size) {
for (int i = 0; i < size; ++i) {
arr[i] *= 2;
}
}

// 5. 函数重载
int sum(int a, int b) {
return a + b;
}

double sum(double a, double b) {
return a + b;
}

int sum(int a, int b, int c) {
return a + b + c;
}

// 6. 内联函数
inline int square(int x) {
return x * x;
}

// 7. constexpr 函数
constexpr int cube(int x) {
return x * x * x;
}

// 8. 函数指针
int subtract(int a, int b) {
return a - b;
}

// 9. 默认参数
void greet(std::string name, std::string message = "Hello") {
std::cout << message << ", " << name << "!" << std::endl;
}

// 10. 可变参数
double average(int count, ...) {
va_list args;
va_start(args, count);

double sum = 0;
for (int i = 0; i < count; ++i) {
sum += va_arg(args, double);
}

va_end(args);
return sum / count;
}

int sumVariadic(int count, ...) {
va_list args;
va_start(args, count);

int sum = 0;
for (int i = 0; i < count; ++i) {
sum += va_arg(args, int);
}

va_end(args);
return sum;
}

// 11. 函数模板
template <typename T>
T maximum(T a, T b) {
return (a > b) ? a : b;
}

// 12. 命名空间中的函数
namespace Math {
int add(int a, int b) {
return a + b;
}

int subtract(int a, int b) {
return a - b;
}
}

// 13. 类成员函数
class Calculator {
public:
int add(int a, int b) {
return a + b;
}

int multiply(int a, int b) {
return a * b;
}
};

// 14. 静态函数
class Utility {
public:
static void staticFunction() {
std::cout << "Static function called: This is a static function" << std::endl;
}

static int counter;
};

int Utility::counter = 0;

// 15. 常量成员函数
class MyClass {
private:
int value;
public:
MyClass(int v) : value(v) {}

int getValue() const {
return value;
}

void setValue(int v) {
value = v;
}
};

// 16. 引用返回值
int& increment(int& x) {
x++;
return x;
}

// 17. 移动语义和右值引用
void processValue(int& lvalue) {
std::cout << "Processing lvalue: " << lvalue << std::endl;
}

void processValue(int&& rvalue) {
std::cout << "Processing rvalue: " << rvalue << std::endl;
}

int main() {
std::cout << "=== 1. 基本函数定义和调用 ===" << std::endl;
sayHello();

int result1 = add(5, 3);
std::cout << "5 + 3 = " << result1 << std::endl;

double result2 = multiply(2.5, 4.0);
std::cout << "2.5 * 4.0 = " << result2 << std::endl;

std::cout << "\n=== 2. 函数参数和返回值 ===" << std::endl;
printInfo("Alice", 30);

std::cout << "\n=== 3. 递归函数 ===" << std::endl;
int fact = factorial(5);
std::cout << "5! = " << fact << std::endl;

std::cout << "\n=== 4. 引用参数 ===" << std::endl;
int x = 10, y = 20;
std::cout << "Before swap: x = " << x << ", y = " << y << std::endl;
swap(x, y);
std::cout << "After swap: x = " << x << ", y = " << y << std::endl;

std::cout << "\n=== 5. 数组参数 ===" << std::endl;
int arr[] = {1, 2, 3, 4, 5};
std::cout << "Before modify: ";
for (int i = 0; i < 5; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;

modifyArray(arr, 5);
std::cout << "After modify: ";
for (int i = 0; i < 5; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;

std::cout << "\n=== 6. 函数重载 ===" << std::endl;
std::cout << "sum(3, 4) = " << sum(3, 4) << std::endl;
std::cout << "sum(2.5, 3.5) = " << sum(2.5, 3.5) << std::endl;
std::cout << "sum(1, 2, 3) = " << sum(1, 2, 3) << std::endl;

std::cout << "\n=== 7. 内联函数 ===" << std::endl;
std::cout << "square(5) = " << square(5) << std::endl;

std::cout << "\n=== 8. constexpr 函数 ===" << std::endl;
std::cout << "cube(3) = " << cube(3) << std::endl;

std::cout << "\n=== 9. Lambda 表达式 ===" << std::endl;
auto lambda = [](int a, int b) { return a - b; };
std::cout << "Lambda: 10 - 3 = " << lambda(10, 3) << std::endl;

std::cout << "\n=== 10. 函数指针 ===" << std::endl;
int (*operation)(int, int) = add;
std::cout << "Using function pointer: 5 + 3 = " << operation(5, 3) << std::endl;

operation = subtract;
std::cout << "Using function pointer with operation: 10 - 4 = " << operation(10, 4) << std::endl;

std::cout << "\n=== 11. 默认参数 ===" << std::endl;
greet("Bob");
greet("Bob", "Welcome");

std::cout << "\n=== 12. 可变参数 ===" << std::endl;
double avg = average(3, 1.0, 2.0, 7.0);
std::cout << "Average of 3 numbers: " << avg << std::endl;

int sumVar = sumVariadic(4, 1, 2, 3, 8);
std::cout << "Sum of 4 numbers: " << sumVar << std::endl;

std::cout << "\n=== 13. 函数模板 ===" << std::endl;
std::cout << "Maximum of 5 and 10: " << maximum(5, 10) << std::endl;
std::cout << "Maximum of 3.14 and 2.71: " << maximum(3.14, 2.71) << std::endl;
std::cout << "Maximum of apple and banana: " << maximum(std::string("apple"), std::string("banana")) << std::endl;

std::cout << "\n=== 14. 命名空间中的函数 ===" << std::endl;
std::cout << "Math::add(5, 3) = " << Math::add(5, 3) << std::endl;
std::cout << "Math::subtract(10, 4) = " << Math::subtract(10, 4) << std::endl;

std::cout << "\n=== 15. 类成员函数 ===" << std::endl;
Calculator calc;
std::cout << "Calculator::add(5, 3) = " << calc.add(5, 3) << std::endl;
std::cout << "Calculator::multiply(4, 6) = " << calc.multiply(4, 6) << std::endl;

std::cout << "\n=== 16. 静态函数 ===" << std::endl;
Utility::staticFunction();
Utility::counter++;
std::cout << "Counter: " << Utility::counter << std::endl;

std::cout << "\n=== 17. 常量成员函数 ===" << std::endl;
MyClass obj(42);
std::cout << "Value: " << obj.getValue() << std::endl;
obj.setValue(100);
std::cout << "Value after modification: " << obj.getValue() << std::endl;

std::cout << "\n=== 18. 引用返回值 ===" << std::endl;
int num = 5;
std::cout << "Value before increment: " << num << std::endl;
int& result3 = increment(num);
std::cout << "Value after increment: " << result3 << std::endl;
increment(result3);
std::cout << "Value after second increment: " << num << std::endl;

std::cout << "\n=== 19. 移动语义和右值引用 ===" << std::endl;
int value = 42;
std::cout << "Original value: " << value << std::endl;
processValue(value); // 传递左值
processValue(100); // 传递右值

return 0;
}