

函数是完成特定功能的代码块,它能让程序结构更清晰、代码更易复用。想象函数就像一个 "工具",我们定义它能做什么,然后在需要时调用它。
函数定义的基本格式:
返回类型 函数名(参数列表) {
// 函数体:实现功能的代码
return 返回值; // 若返回类型为void则无此句
}代码示例:基本函数定义
#include <iostream>
using namespace std;
// 函数定义:计算两个整数的和
int add(int a, int b) { // 返回类型int,函数名add,参数列表(int a, int b)
int result = a + b; // 函数体:计算和
return result; // 返回计算结果
}
// 函数定义:打印欢迎信息(无返回值)
void printWelcome() { // 返回类型void表示无返回值
cout << "欢迎学习C++函数!" << endl;
// 无return语句(或return;)
}
int main() {
printWelcome(); // 调用无返回值函数
int sum = add(3, 5); // 调用有返回值函数
cout << "3 + 5 = " << sum << endl;
return 0;
}
函数调用的基本流程:声明函数 → 调用函数 → 执行函数体 → 返回结果。
函数声明的作用:告诉编译器函数的返回类型、函数名和参数列表,确保调用时类型匹配。
代码示例:函数声明与调用
#include <iostream>
using namespace std;
// 函数声明(原型)
int max(int a, int b); // 只需要参数类型,参数名可省略:int max(int, int);
int main() {
int x = 10, y = 20;
// 函数调用:使用函数名+参数列表
int larger = max(x, y);
cout << "较大的数是:" << larger << endl;
return 0;
}
// 函数定义(实现)
int max(int a, int b) {
return (a > b) ? a : b; // 三目运算符返回较大值
}运行结果:

C++ 中参数传递主要有两种方式:值传递和引用传递。
&符号。代码示例:参数传递方式对比
#include <iostream>
using namespace std;
// 1. 值传递:修改形参不影响实参
void valuePass(int num) {
num = 100; // 只修改形参
cout << "函数内(值传递):num = " << num << endl;
}
// 2. 引用传递:修改形参影响实参
void referencePass(int &num) {
num = 200; // 修改实参(因为形参是实参的别名)
cout << "函数内(引用传递):num = " << num << endl;
}
int main() {
int a = 10, b = 20;
// 测试值传递
cout << "值传递测试:" << endl;
cout << "调用前:a = " << a << endl;
valuePass(a);
cout << "调用后:a = " << a << endl; // a的值未改变
// 测试引用传递
cout << "\n引用传递测试:" << endl;
cout << "调用前:b = " << b << endl;
referencePass(b);
cout << "调用后:b = " << b << endl; // b的值已改变
return 0;
}运行结果:

#include <iostream>
#include <string>
using namespace std;
// 结构体存储学生信息
struct Student {
string name;
int score;
};
// 引用传递:修改学生成绩
void updateScore(Student &stu, int newScore) {
stu.score = newScore;
}
// 值传递:打印学生信息(不需要修改原数据)
void printStudent(Student stu) {
cout << "姓名:" << stu.name << ",成绩:" << stu.score << endl;
}
int main() {
Student s = {"张三", 75};
cout << "修改前:";
printStudent(s);
// 修改成绩(引用传递)
updateScore(s, 92);
cout << "修改后:";
printStudent(s);
return 0;
}运行结果:

内联函数(Inline Function)是一种特殊的函数,编译器会将内联函数的代码 "嵌入" 到调用它的地方,减少函数调用的开销(如压栈、跳转等操作)。
inline关键字声明代码示例:内联函数的使用
#include <iostream>
using namespace std;
// 声明为内联函数:求两个数的较小值
inline int min(int a, int b) {
return (a < b) ? a : b;
}
int main() {
int x = 15, y = 8;
// 调用内联函数
int smaller = min(x, y);
cout << x << "和" << y << "中较小的是:" << smaller << endl;
return 0;
}运行结果:

constexpr函数是 C++11 引入的特性,用于在编译期计算常量表达式,提高程序运行效率。
代码示例:constexpr 函数的使用
#include <iostream>
using namespace std;
// constexpr函数:编译期计算阶乘
constexpr int factorial(int n) {
return (n <= 1) ? 1 : (n * factorial(n - 1));
}
int main() {
// 1. 编译期计算(结果在编译时就已确定)
constexpr int fact5 = factorial(5); // 编译期计算5! = 120
cout << "5的阶乘(编译期计算):" << fact5 << endl;
// 2. 运行期计算(如果参数不是常量表达式)
int n;
cout << "请输入一个整数:";
cin >> n;
int factN = factorial(n); // 运行期计算n!
cout << n << "的阶乘(运行期计算):" << factN << endl;
return 0;
}运行结果:

C++ 允许为函数参数指定默认值,当调用函数时如果不提供该参数,则使用默认值。
代码示例:带默认参数的函数
#include <iostream>
#include <string>
using namespace std;
// 带默认参数的函数声明(默认参数在声明中设置)
void printInfo(string name, int age = 18, string gender = "未知");
// 函数定义(不需要重复默认参数)
void printInfo(string name, int age, string gender) {
cout << "姓名:" << name << ",年龄:" << age << ",性别:" << gender << endl;
}
int main() {
// 1. 提供所有参数
printInfo("张三", 20, "男");
// 2. 省略最后一个参数(使用gender的默认值)
printInfo("李四", 22);
// 3. 省略后两个参数(使用age和gender的默认值)
printInfo("王五");
return 0;
}运行结果:

// 错误:不能跳过前面的参数给后面的参数设默认值
void wrongFunc(int a, int b = 5, int c) { // 编译错误!
// ...
}函数重载(Function Overloading)允许在同一作用域内定义多个函数名相同但参数列表不同的函数,提高代码可读性和复用性。
代码示例:函数重载的实现
#include <iostream>
#include <string>
#include <sstream> // 为兼容性添加
using namespace std;
// 1. 两个int相加
int add(int a, int b) {
cout << "int + int:";
return a + b;
}
// 2. 两个double相加(参数类型不同)
double add(double a, double b) {
cout << "double + double:";
return a + b;
}
// 3. 三个int相加(参数数量不同)
int add(int a, int b, int c) {
cout << "int + int + int:";
return a + b + c;
}
// 4. int和string拼接(参数类型和顺序不同)
string add(int num, string str) {
cout << "int + string:";
// 方法1:使用to_string(需要C++11支持)
// return to_string(num) + str;
// 方法2:使用stringstream(兼容性更好)
stringstream ss;
ss << num;
return ss.str() + str;
}
int main() {
cout << add(3, 5) << endl; // 调用int+int版本
cout << add(2.5, 3.8) << endl; // 调用double+double版本
cout << add(10, 20, 30) << endl; // 调用int+int+int版本
cout << add(100, "分") << endl; // 调用int+string版本
return 0;
}运行结果:

#include <iostream>
using namespace std;
// 计算器类
class Calculator {
public:
// 加法重载
int calculate(int a, int b) {
return a + b;
}
double calculate(double a, double b) {
return a + b;
}
// 减法重载
int calculate(int a, int b, char op) { // 参数数量不同
if (op == '-') return a - b;
return 0;
}
double calculate(double a, double b, char op) {
if (op == '-') return a - b;
return 0;
}
};
int main() {
Calculator calc;
cout << "3 + 5 = " << calc.calculate(3, 5) << endl;
cout << "2.5 + 4.8 = " << calc.calculate(2.5, 4.8) << endl;
cout << "10 - 4 = " << calc.calculate(10, 4, '-') << endl;
cout << "8.5 - 3.2 = " << calc.calculate(8.5, 3.2, '-') << endl;
return 0;
}运行结果:

C++ 标准库提供了丰富的系统函数(如数学运算、字符串处理等),使用时需要包含对应的头文件。
功能类别 | 头文件 | 常用函数 |
|---|---|---|
数学运算 | <cmath> | sqrt()(平方根)、pow()(幂运算)、sin()(正弦) |
随机数 | <cstdlib> | rand()(生成随机数)、srand()(设置随机数种子) |
字符串转换 | <string> | to_string()(数值转字符串) |
代码示例:系统函数的使用
#include <iostream>
#include <cmath> // 数学函数
#include <cstdlib> // 随机数函数
#include <ctime> // 时间函数(用于设置随机数种子)
#include <string> // 字符串转换函数
#include <sstream> // 为兼容性添加
using namespace std;
int main() {
// 1. 数学函数
double num = 25.0;
cout << num << "的平方根是:" << sqrt(num) << endl; // 平方根
cout << "2的3次方是:" << pow(2, 3) << endl; // 幂运算
// 修改:使用更通用的π表示方法
const double PI = 3.14159265358979323846;
cout << "π/2的正弦值是:" << sin(PI / 2) << endl; // 正弦函数
// 2. 随机数函数
srand(time(0)); // 设置随机数种子(基于当前时间,确保每次运行结果不同)
cout << "\n生成3个1-100的随机数:";
for (int i = 0; i < 3; i++) {
// rand() % 100 生成0-99的随机数,+1后变为1-100
cout << (rand() % 100 + 1) << " ";
}
cout << endl;
// 3. 字符串转换函数
int age = 25;
// 方法1:使用to_string(需要C++11支持)
// string ageStr = to_string(age) + "岁";
// 方法2:使用stringstream(兼容性更好)
stringstream ss;
ss << age;
string ageStr = ss.str() + "岁";
cout << "\n转换结果:" << ageStr << endl;
return 0;
}运行结果(随机数部分每次运行不同):

函数调用的过程依赖于内存中的 "运行栈"(Runtime Stack),每次函数调用都会创建一个 "栈帧"(Stack Frame)存储函数的参数、返回地址和局部变量。
高地址
+-------------------+
| ... |
+-------------------+
| func()的局部变量 |
+-------------------+
| 返回地址 | ← 栈帧信息
+-------------------+
| 参数b的值 |
+-------------------+
| 参数a的值 |
+-------------------+
| main()的局部变量 |
| ... |
+-------------------+
低地址代码示例:观察函数调用的栈行为
#include <iostream>
using namespace std;
// 展示栈帧中局部变量的特性(函数调用结束后局部变量被释放)
int stackDemo(int x) {
int y = x * 2; // 局部变量y
cout << "stackDemo中:x=" << x << ", y=" << y << endl;
return y;
}
int main() {
int a = 10; // main函数的局部变量
cout << "main中调用前:a=" << a << endl;
int result = stackDemo(a); // 调用函数,创建新栈帧
cout << "main中调用后:result=" << result << endl;
// cout << y << endl; // 错误:y是stackDemo的局部变量,此处不可访问
return 0;
}运行结果:

函数声明确保编译器在函数调用时进行类型检查,避免类型不匹配的错误,提高代码安全性。
#include <iostream>
using namespace std;
int main() {
// 错误:调用未声明的函数(编译器可能警告或报错)
int result = unsafeFunc(3.14); // 实参是double,函数期望int
cout << "结果:" << result << endl;
return 0;
}
// 函数定义在调用之后且未声明
int unsafeFunc(int num) {
return num * 2;
}#include <iostream>
using namespace std;
// 函数声明:明确参数类型和返回类型
int safeFunc(int num); // 声明必须在调用之前
int main() {
// 编译器会检查类型匹配
// int result = safeFunc(3.14); // 编译错误:实参类型不匹配(double不能隐式转换为int)
int result = safeFunc(static_cast<int>(3.14)); // 显式类型转换后调用
cout << "结果:" << result << endl;
return 0;
}
// 函数定义
int safeFunc(int num) {
return num * 2;
}运行结果:
结果:6本章我们系统学习了 C++ 函数的核心知识,包括:
inline关键字减少调用开销,适用于短小函数。constexpr函数:在编译期计算常量表达式,提高运行效率。函数是 C++ 程序的基本组成单元,掌握函数的各种特性和使用技巧,是编写高效、可维护代码的基础。建议多动手实践本章的代码示例,深入理解函数在内存中的执行机制。
如果有任何问题或建议,欢迎在评论区留言交流!