显示页面讨论反向链接回到顶部 本页面只读。您可以查看源文件,但不能更改它。如果您觉得这是系统错误,请联系管理员。 ====== 函数 (Functions) ====== ===== 概述 ===== **作用**:将一段经常使用的代码封装起来,**减少重复代码**。 一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能。 ===== 函数的定义 ===== 函数的定义一般主要有5个步骤: - 返回值类型 - 函数名 - 参数列表 - 函数体语句 - return 表达式 **语法**: <code cpp> 返回值类型 函数名 (参数列表) { 函数体语句 return表达式 } </code> * **返回值类型**:一个函数可以返回一个值。在函数定义中。 * **函数名**:给函数起个名称。 * **参数列表**:使用该函数时,传入的数据。 * **函数体语句**:花括号内的代码,函数内需要执行的语句。 * **return表达式**:和返回值类型挂钩,函数执行完后,返回相应的数据。 **示例**:定义一个加法函数,实现两个数相加。 <code cpp> // 函数定义 int add(int num1, int num2) { int sum = num1 + num2; return sum; } </code> ===== 函数的调用 ===== **功能**:使用定义好的函数。 **语法**:<code cpp>函数名 (参数)</code> **示例**: <code cpp> // 函数定义 int add(int num1, int num2) // 定义中的num1, num2称为形式参数,简称形参 { int sum = num1 + num2; return sum; } int main() { int a = 10; int b = 10; // 调用add函数 int sum = add(a, b); // 调用时的a, b称为实际参数,简称实参 cout << "sum = " << sum << endl; a = 100; b = 100; sum = add(a, b); cout << "sum = " << sum << endl; system("pause"); return 0; } </code> > **总结**:函数定义里小括号内称为形参,函数调用时传入的参数称为实参。 ===== 值传递 ===== * 所谓值传递,就是函数调用时实参将数值传入给形参。 * 值传递时,**如果形参发生,并不会影响实参**。 **示例**: <code cpp> void swap(int num1, int num2) { cout << "交换前:" << endl; cout << "num1 = " << num1 << endl; cout << "num2 = " << num2 << endl; int temp = num1; num1 = num2; num2 = temp; cout << "交换后:" << endl; cout << "num1 = " << num1 << endl; cout << "num2 = " << num2 << endl; // return ; 当函数声明时候,不需要返回值,可以不写return } int main() { int a = 10; int b = 20; swap(a, b); cout << "mian中的 a = " << a << endl; cout << "mian中的 b = " << b << endl; system("pause"); return 0; } </code> > **解释**:在 `swap` 函数内部,`num1` 和 `num2` 确实交换了值,但这只是形参的改变。`main` 函数中的实参 `a` 和 `b` 的值并没有发生改变,这就是值传递的特性。 ===== 函数的常见样式 ===== 常见的函数样式有4种: - 无参无返 - 有参无返 - 无参有返 - 有参有返 **示例**: <code cpp> // 函数常见样式 // 1、 无参无返 void test01() { // void a = 10; // 无类型不可以创建变量,原因无法分配内存 cout << "this is test01" << endl; } // 2、 有参无返 void test02(int a) { cout << "this is test02" << endl; cout << "a = " << a << endl; } // 3、 无参有返 int test03() { cout << "this is test03 " << endl; return 10; } // 4、 有参有返 int test04(int a, int b) { cout << "this is test04 " << endl; int sum = a + b; return sum; } </code> ===== 函数的声明 ===== **作用**:告诉编译器函数名称及如何调用函数。函数的实际主体可以单独定义。 * **函数的声明可以多次,但是函数的定义只能有一次** **示例**: <code cpp> // 声明可以多次,定义只能一次 // 声明 int max(int a, int b); int max(int a, int b); // 定义 int max(int a, int b) { return a > b ? a : b; } int main() { int a = 100; int b = 200; cout << max(a, b) << endl; system("pause"); return 0; } </code> > **注意**:如果函数定义在 `main` 函数之后,必须在 `main` 函数之前写函数声明,否则编译器找不到该函数。 ===== 函数的分文件编写 ===== **作用**:让代码结构更加清晰。 函数分文件编写一般有4个步骤: - 创建后缀名为 `.h` 的头文件 - 创建后缀名为 `.cpp` 的源文件 - 在头文件中写函数的声明 - 在源文件中写函数的定义 **示例**: **1. swap.h 文件** <code cpp> // swap.h文件 #include<iostream> using namespace std; // 实现两个数字交换的函数声明 void swap(int a, int b); </code> **2. swap.cpp 文件** <code cpp> // swap.cpp文件 #include "swap.h" void swap(int a, int b) { int temp = a; a = b; b = temp; cout << "a = " << a << endl; cout << "b = " << b << endl; } </code> **3. main函数文件** <code cpp> // main函数文件 #include "swap.h" int main() { int a = 100; int b = 200; swap(a, b); system("pause"); return 0; } </code> ===== 函数提高 ===== ==== 函数默认参数 ==== 在C++中,函数的形参列表中的形参是可以有默认值的。 **语法**:<code cpp>返回值类型 函数名 (参数 = 默认值) {}</code> **示例代码:** <code cpp> #include <iostream> using namespace std; // 函数默认参数 // 如果我们自己传入数据,就用自己的数据,如果没有,那么就用默认值 // 语法: 返回值类型 函数名 (形参 = 默认值) {} int func(int a, int b = 10, int c = 10) { return a + b + c; } // 注意事项: // 1. 如果某个位置参数有默认值,那么从这个位置往后,从左向右,必须都要有默认值 // int func2(int a = 10, int b = 10, int c) { // 错误,c没有默认值 // return a + b + c; // } // 2. 如果函数声明有默认值,函数实现的时候就不能有默认参数 // 声明和实现只能有一个有默认参数 int func2(int a = 10, int b = 10); int func2(int a, int b) { return a + b; } int main() { cout << "ret = " << func(20, 20) << endl; // 传入两个参数,c使用默认值 cout << "ret = " << func(100) << endl; // 传入一个参数,b和c使用默认值 system("pause"); return 0; } </code> ==== 函数占位参数 ==== C++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置。 **语法**:<code cpp>返回值类型 函数名 (数据类型) {}</code> 在现阶段函数的占位参数存在意义不大,但是后面的课程中会用到该技术(如重载前置和后置递增运算符)。 **示例代码:** <code cpp> #include <iostream> using namespace std; // 函数占位参数,占位参数也可以有默认参数 void func(int a, int = 10) { cout << "this is func" << endl; } int main() { // func(10, 10); // 占位参数必须填补 func(10); // 如果占位参数有默认值,可以不填补 system("pause"); return 0; } </code> ==== 函数重载 ==== === 函数重载概述 === **作用**:函数名可以相同,提高复用性。 **函数重载满足条件**: * 同一个作用域下 * 函数名称相同 * 函数参数**类型不同** 或者 **个数不同** 或者 **顺序不同** **注意**:函数的返回值不可以作为函数重载的条件。 **示例代码:** <code cpp> #include <iostream> using namespace std; // 函数重载需要函数都在同一个作用域下 void func() { cout << "func 的调用!" << endl; } void func(int a) { cout << "func (int a) 的调用!" << endl; } void func(double a) { cout << "func (double a)的调用!" << endl; } void func(int a, double b) { cout << "func (int a ,double b) 的调用!" << endl; } void func(double a, int b) { cout << "func (double a ,int b)的调用!" << endl; } // 函数返回值不可以作为函数重载条件 // int func(double a, int b) // { // cout << "func (double a ,int b)的调用!" << endl; // } int main() { func(); func(10); func(3.14); func(10, 3.14); func(3.14, 10); system("pause"); return 0; } </code> === 函数重载注意事项 === * 引用作为重载条件 * 函数重载碰到函数默认参数 **示例代码:** <code cpp> #include <iostream> using namespace std; // 函数重载注意事项 // 1、引用作为重载条件 void func(int &a) // int &a = 10; 不合法 { cout << "func (int &a) 调用 " << endl; } void func(const int &a) // const int &a = 10; 合法 { cout << "func (const int &a) 调用 " << endl; } // 2、函数重载碰到函数默认参数 void func2(int a, int b = 10) { cout << "func2(int a, int b = 10) 调用" << endl; } void func2(int a) { cout << "func2(int a) 调用" << endl; } int main() { int a = 10; func(a); // 调用无const,因为a是变量,可读可写 func(10);// 调用有const,因为10是常量 // func2(10); // 碰到默认参数产生歧义,需要避免 // 编译器不知道是调用带默认参数的func2,还是只带一个int参数的func2 system("pause"); return 0; } </code> 登录 Detach Close 该主题尚不存在 您访问的页面并不存在。如果允许,您可以使用创建该页面按钮来创建它。 cplus/函数.txt 最后更改: 2025/11/28 13:09由 张叶安 登录