Skip to content

C++ 函数

函数是执行特定任务的代码块,可以被重复调用。使用函数可以提高代码的可读性、可维护性和可重用性。

1. 函数的定义

1.1 基本语法

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

1.2 示例

cpp
#include <iostream>

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

int main() {
    int result = add(10, 20);
    std::cout << "10 + 20 = " << result << std::endl;
    return 0;
}

2. 函数的声明

函数声明告诉编译器函数的名称、返回类型和参数列表,但不包含函数体。

2.1 示例

cpp
#include <iostream>

// 函数声明
int add(int a, int b);
void print_hello();

int main() {
    int result = add(10, 20);
    std::cout << "10 + 20 = " << result << std::endl;
    print_hello();
    return 0;
}

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

void print_hello() {
    std::cout << "Hello, World!" << std::endl;
}

3. 函数的参数

3.1 值传递

值传递是将参数的值复制给函数,函数内部对参数的修改不会影响原始值。

cpp
#include <iostream>

void swap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 10;
    int y = 20;
    
    std::cout << "交换前: x = " << x << ", y = " << y << std::endl;
    swap(x, y);
    std::cout << "交换后: x = " << x << ", y = " << y << std::endl;
    
    return 0;
}

3.2 引用传递

引用传递是将参数的引用传递给函数,函数内部对参数的修改会影响原始值。

cpp
#include <iostream>

void swap(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 10;
    int y = 20;
    
    std::cout << "交换前: x = " << x << ", y = " << y << std::endl;
    swap(x, y);
    std::cout << "交换后: x = " << x << ", y = " << y << std::endl;
    
    return 0;
}

3.3 指针传递

指针传递是将参数的地址传递给函数,函数内部可以通过指针修改原始值。

cpp
#include <iostream>

void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 10;
    int y = 20;
    
    std::cout << "交换前: x = " << x << ", y = " << y << std::endl;
    swap(&x, &y);
    std::cout << "交换后: x = " << x << ", y = " << y << std::endl;
    
    return 0;
}

3.4 默认参数

默认参数是在函数声明中为参数指定默认值,调用函数时可以省略这些参数。

cpp
#include <iostream>

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

int main() {
    std::cout << "add(10) = " << add(10) << std::endl;      // 20
    std::cout << "add(10, 20) = " << add(10, 20) << std::endl;  // 30
    
    return 0;
}

4. 函数的返回值

4.1 返回值类型

函数可以返回各种类型的值,包括基本数据类型、指针、引用等。

cpp
#include <iostream>

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

double divide(double a, double b) {
    return a / b;
}

bool is_even(int n) {
    return (n % 2 == 0);
}

int main() {
    std::cout << "add(10, 20) = " << add(10, 20) << std::endl;
    std::cout << "divide(10.0, 3.0) = " << divide(10.0, 3.0) << std::endl;
    std::cout << "is_even(10) = " << is_even(10) << std::endl;
    
    return 0;
}

4.2 void 类型

void类型表示函数不返回任何值。

cpp
#include <iostream>

void print_hello() {
    std::cout << "Hello, World!" << std::endl;
}

int main() {
    print_hello();
    return 0;
}

4.3 返回引用

函数可以返回引用,允许函数调用作为左值。

cpp
#include <iostream>

int& get_element(int arr[], int index) {
    return arr[index];
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    
    get_element(arr, 2) = 100;  // 修改数组的第三个元素
    
    for (int i = 0; i < 5; i++) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;
    
    return 0;
}

5. 函数重载

函数重载允许定义多个同名函数,只要它们的参数列表不同。

5.1 示例

cpp
#include <iostream>

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

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

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

int main() {
    std::cout << "add(10, 20) = " << add(10, 20) << std::endl;
    std::cout << "add(10.5, 20.5) = " << add(10.5, 20.5) << std::endl;
    std::cout << "add(10, 20, 30) = " << add(10, 20, 30) << std::endl;
    
    return 0;
}

6. 递归函数

递归函数是调用自身的函数。

6.1 示例

cpp
#include <iostream>

int factorial(int n) {
    if (n <= 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

int main() {
    int n = 5;
    std::cout << n << "! = " << factorial(n) << std::endl;
    
    return 0;
}

7. 内联函数

内联函数使用inline关键字,建议编译器将函数调用展开为函数体,以减少函数调用的开销。

7.1 示例

cpp
#include <iostream>

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

int main() {
    std::cout << "add(10, 20) = " << add(10, 20) << std::endl;
    return 0;
}

8. constexpr 函数(C++11)

constexpr函数可以在编译时计算其返回值。

8.1 示例

cpp
#include <iostream>

constexpr int square(int n) {
    return n * n;
}

int main() {
    constexpr int result = square(5);
    std::cout << "5 的平方: " << result << std::endl;
    
    int arr[square(3)];  // 使用 constexpr 函数定义数组大小
    
    return 0;
}

9. lambda 表达式(C++11)

lambda 表达式是一种匿名函数,可以就地定义函数。

9.1 基本语法

cpp
[capture](parameters) -> return_type { body }

9.2 示例

cpp
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    // 简单的 lambda 表达式
    auto add = [](int a, int b) {
        return a + b;
    };
    std::cout << "add(10, 20) = " << add(10, 20) << std::endl;
    
    // 使用 lambda 表达式排序
    std::vector<int> vec = {5, 2, 8, 1, 9};
    std::sort(vec.begin(), vec.end(), [](int a, int b) {
        return a > b;
    });
    
    for (int x : vec) {
        std::cout << x << " ";
    }
    std::cout << std::endl;
    
    return 0;
}

10. 函数指针

函数指针是指向函数的指针,可以用于回调函数等场景。

10.1 示例

cpp
#include <iostream>

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

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

int calculate(int a, int b, int (*operation)(int, int)) {
    return operation(a, b);
}

int main() {
    int result1 = calculate(10, 20, add);
    int result2 = calculate(10, 20, subtract);
    
    std::cout << "10 + 20 = " << result1 << std::endl;
    std::cout << "10 - 20 = " << result2 << std::endl;
    
    return 0;
}

11. 示例:综合运用

现在,让我们看一个综合运用各种函数特性的例子:

cpp
#include <iostream>
#include <vector>
#include <algorithm>

// 函数声明
int add(int a, int b);
void swap(int& a, int& b);
int factorial(int n);
inline int square(int n);
constexpr int cube(int n);

int main() {
    // 基本函数调用
    std::cout << "基本函数调用:" << std::endl;
    std::cout << "add(10, 20) = " << add(10, 20) << std::endl;
    std::cout << std::endl;
    
    // 引用传递
    std::cout << "引用传递:" << std::endl;
    int x = 10;
    int y = 20;
    std::cout << "交换前: x = " << x << ", y = " << y << std::endl;
    swap(x, y);
    std::cout << "交换后: x = " << x << ", y = " << y << std::endl;
    std::cout << std::endl;
    
    // 递归函数
    std::cout << "递归函数:" << std::endl;
    int n = 5;
    std::cout << n << "! = " << factorial(n) << std::endl;
    std::cout << std::endl;
    
    // 内联函数
    std::cout << "内联函数:" << std::endl;
    std::cout << "square(5) = " << square(5) << std::endl;
    std::cout << std::endl;
    
    // constexpr 函数
    std::cout << "constexpr 函数:" << std::endl;
    constexpr int result = cube(3);
    std::cout << "3 的立方: " << result << std::endl;
    std::cout << std::endl;
    
    // lambda 表达式
    std::cout << "lambda 表达式:" << std::endl;
    auto multiply = [](int a, int b) {
        return a * b;
    };
    std::cout << "multiply(10, 20) = " << multiply(10, 20) << std::endl;
    
    // 使用 lambda 表达式排序
    std::vector<int> vec = {5, 2, 8, 1, 9};
    std::sort(vec.begin(), vec.end(), [](int a, int b) {
        return a > b;
    });
    
    std::cout << "降序排序: ";
    for (int x : vec) {
        std::cout << x << " ";
    }
    std::cout << std::endl;
    
    return 0;
}

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

void swap(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

int factorial(int n) {
    if (n <= 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

inline int square(int n) {
    return n * n;
}

constexpr int cube(int n) {
    return n * n * n;
}

小结

C++ 函数包括:

  1. 函数的定义

    • 基本语法:返回类型 函数名(参数列表) { 函数体 }
  2. 函数的声明

    • 告诉编译器函数的名称、返回类型和参数列表
  3. 函数的参数

    • 值传递:复制参数的值
    • 引用传递:传递参数的引用
    • 指针传递:传递参数的地址
    • 默认参数:为参数指定默认值
  4. 函数的返回值

    • 返回各种类型的值
    • void类型:不返回任何值
    • 返回引用:允许函数调用作为左值
  5. 函数重载

    • 定义多个同名函数,参数列表不同
  6. 递归函数

    • 调用自身的函数
  7. 内联函数

    • 使用inline关键字,建议编译器展开函数调用
  8. constexpr 函数(C++11):

    • 可以在编译时计算返回值
  9. lambda 表达式(C++11):

    • 匿名函数,可以就地定义
  10. 函数指针

    • 指向函数的指针

关键概念:

  • 函数声明:告诉编译器函数的接口
  • 函数定义:提供函数的实现
  • 参数传递:值传递、引用传递、指针传递
  • 返回值:函数执行的结果

掌握函数是编写 C++ 程序的基础,在后续章节中,我们将学习 C++ 的数字和数组。