第九课 函数

函数是重复使用的代码放在一起,实现某一种功能,方便代码组织和调用。例如,cout是C++内置的输出函数。学会使用函数是模块化编程的基础。

一、函数定义

返回类型 函数名(参数列表) {
    // 函数体
    return 结果;
}

函数示例 – 加法函数

#include <iostream>
using namespace std;

// 定义一个加法函数
int add(int a, int b) {
    return a + b;
}

int main() {
    // 调用函数
    int result = add(3, 5);
    cout << result << endl;  // 输出8

    // 多次调用
    cout << add(10, 20) << endl;  // 30
    cout << add(100, 200) << endl;  // 300

    return 0;
}

二、函数的优点

  • 代码复用:相同功能只需写一次
  • 模块化:把大问题拆成小问题
  • 可读性:函数名表达功能,见名知意
  • 可维护性:修改只需改一处

三、函数分类

  1. 有返回值函数
#include <iostream>
using namespace std;

// 计算两个数的最大值
int max(int a, int b) {
    if (a > b) {
        return a;
    } else {
        return b;
    }
}

// 计算阶乘
int factorial(int n) {
    int result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }
    return result;
}

int main() {
    cout << max(10, 20) << endl;      // 20
    cout << factorial(5) << endl;     // 120

    return 0;
}
  1. 无返回值函数(void)
#include <iostream>
using namespace std;

// 打印三角形
void printTriangle(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            cout << "* ";
        }
        cout << endl;
    }
}

// 交换两个数(通过引用)
void swap(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    printTriangle(5);
    // 输出:
    // *
    // * *
    // * * *
    // * * * *
    // * * * * *

    int x = 10, y = 20;
    swap(x, y);
    cout << x << " " << y << endl;  // 20 10

    return 0;
}
  1. 带默认参数的函数
#include <iostream>
using namespace std;

// 函数参数默认值
void printInfo(string name, int age = 18) {
    cout << "姓名:" << name << ",年龄:" << age << endl;
}

int main() {
    printInfo("Tom");           // 年龄使用默认值18
    printInfo("Lucy", 20);     // 指定年龄20
    printInfo("Mike", 25);     // 指定年龄25

    return 0;
}
  1. 函数重载(同名不同参数,后续学习)
#include <iostream>
using namespace std;

// 同名函数,不同参数
int add(int a, int b) {
    cout << "int版本:" ;
    return a + b;
}

double add(double a, double b) {
    cout << "double版本:" ;
    return a + b;
}

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

int main() {
    cout << add(1, 2) << endl;          // 3
    cout << add(1.5, 2.5) << endl;     // 4
    cout << add("Hello", "World") << endl;  // HelloWorld

    return 0;
}
  1. 递归函数(自己调用自己,后续学习)
#include <iostream>
using namespace std;

// 计算阶乘(递归版本)
int factorial(int n) {
    if (n <= 1) {
        return 1;  // 递归终止条件
    }
    return n * factorial(n - 1);  // 递归调用
}

// 斐波那契数列
int fibonacci(int n) {
    if (n <= 1) {
        return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
    cout << factorial(5) << endl;   // 120
    cout << fibonacci(6) << endl;  // 8 (0+1+1+2+3+5=12... wait, 0,1,1,2,3,5,8)

    // 打印前10个斐波那契数
    for (int i = 0; i < 10; i++) {
        cout << fibonacci(i) << " ";
    }
    // 输出:0 1 1 2 3 5 8 13 21 34

    return 0;
}
  1. 函数模板(泛型函数,后续学习)
#include <iostream>
using namespace std;

// 函数模板:可以处理任意类型
template<typename T>
T maximum(T a, T b) {
    return (a > b) ? a : b;
}

int main() {
    // 自动推断类型
    cout << maximum(10, 20) << endl;        // 20
    cout << maximum(3.5, 2.8) << endl;      // 3.5
    cout << maximum('A', 'B') << endl;      // B

    return 0;
}

四、全局变量和局部变量

#include <iostream>
using namespace std;

int globalVar = 100;  // 全局变量(函数外部)

void test() {
    int localVar = 10;  // 局部变量(函数内部)
    cout << "局部变量:" << localVar << endl;
    cout << "全局变量:" << globalVar << endl;
}

int main() {
    cout << "全局变量:" << globalVar << endl;  // 100

    test();  // 调用函数

    // cout << localVar;  // 错误:局部变量不能在函数外使用

    return 0;
}

五、静态变量

#include <iostream>
using namespace std;

// 静态变量:函数调用之间保持值
void counter() {
    static int count = 0;  // 第一次调用初始化,之后保持值
    count++;
    cout << "调用次数:" << count << endl;
}

int main() {
    counter();  // 1
    counter();  // 2
    counter();  // 3

    return 0;
}

类似文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注