4.1 函数的基础知识

4.1.1 什么是 c 语言函数

In C programming, a function is a self-contained block of code that performs a specific task or a set of related tasks.一块自包含的代码,用于执行特定任务或一组相关任务

介绍两个概念;

函数声明 function declaration,使得编译器可以先对函数进行概要浏览,而函数的完整定义以后再给出。

函数原型 function prototype,为如何调用函数提供了完整描述。

4.1.2 c 语言为什么要设计函数

函数是一组执行特定任务的语句的集合,它们可以独立调用并且可以返回结果。 定义函数的目的和用途:

  1. 模块化和结构化编程:函数允许将代码划分为更小、更容易管理的模块。每个函数负责执行特定的任务或完成特定的功能,这有助于将整个程序分解为更小的、可管理的部分,提高了代码的结构性和可维护性。

  2. 代码重用:一旦你定义了一个函数,你可以在程序的不同部分多次调用它,而不必重复编写相同的代码。这样可以节省时间,减少错误,提高代码的可维护性。

  3. 抽象和封装:函数允许你隐藏代码的细节,只暴露必要的接口和功能。这有助于提高代码的抽象水平,使其更容易理解和使用。

  4. 提高可读性:将代码分解为函数,每个函数都有明确的目的和功能,这有助于提高代码的可读性。阅读一个函数比阅读整个程序更容易理解。

  5. 调试和测试:函数可以独立地测试和调试,这使得在发现问题时更容易定位和修复错误。你可以集中精力检查特定函数,而不必检查整个程序。

  6. 提高团队协作:在大型项目中,多个开发人员通常合作开发。函数的使用可以使每个开发人员负责实现特定的功能模块,从而提高团队的协作效率。

  7. 提高性能:函数可以编写得更高效,因为你可以专注于优化特定功能而不必担心整个程序。这有助于提高程序的性能。

4.1.3 怎么从数学和生活中的例子去理解函数

数学中的函数:

在数学中,函数是一种将输入映射到输出的规则。例如,考虑一个简单的数学函数:

f(x) = 2x

这个函数将输入的数字 x 乘以 2,并返回结果。例如,如果你将 x 设为 3,那么函数的输出就是 6。这里的关键点是,对于相同的输入 x,函数总是返回相同的输出值。

类似地,在编程中,函数也是这样工作的。你传递输入参数给函数,它执行一些操作,并返回结果。这种把输入映射到输出的概念与数学中的函数非常相似。

生活中的例子:

让我们考虑一个生活中的例子,比如制作三明治:

  1. 函数定义:在制作三明治之前,你需要准备一些食材和工具。这就像在编程中定义一个函数,你需要指定函数的名称和需要的输入(食材)以及执行的操作。

  2. 函数调用:一旦你准备好所有食材,你就可以开始制作三明治了。这就像在编程中调用一个函数,传递给它所需的参数。

  3. 函数操作:在制作三明治的过程中,你执行一系列步骤,比如把面包涂上黄油、放上火腿和奶酪、加点蔬菜等。这就像函数内部执行的操作。

  4. 返回结果:最终,你制作完成了一个美味的三明治,这就是函数的结果。在编程中,函数也会返回一个结果,你可以将其用于后续操作。

总之,函数可以在编程中类比为数学中的函数,它们接受输入,执行操作,并返回结果。生活中的例子如制作三明治也可以帮助你理解这个概念。函数是将大型任务分解为可管理部分、提高可重用性和模块化的关键工具。

4.1.4 如何去定义一个函数

注意:留空可以用于在开发时用于保留位置 as a place holder

返回类型 函数名(参数列表) {
    // 函数体
    // 可执行的C语句
    return 返回值; // 如果函数有返回值的话
}
int add(int a, int b) {
    int sum = a + b;
    return sum;
}

4.1.5 函数组成的一些知识

  1. Function Definition 函数定义 : A function is defined with a specific name, a return type, and an optional list of parameters. 参数 The return type specifies the data type of the value that the function will return, and the parameters represent the inputs to the function.

  2. Function Call 函数调用: To execute a function, you use its name followed by parentheses, which may contain arguments if the function takes parameters. For example, functionName(arguments);.

  3. Function Prototype 函数原型 : A function prototype provides the compiler with information about the function's name, return type, and the types of its parameters. It allows you to declare a function before defining it, making it possible to call functions in any order.

函数原型看不懂,先不纠结。

  1. Function Parameters 函数参数 : Parameters are variables declared within the parentheses 括号 of a function definition. They act as placeholders for the values that are passed to the function when it is called. Functions can have zero or more parameters.

  2. Return Statement : The return statement is used to exit a function and return a value of the specified data type to the caller. If a function is declared as returning void, it doesn't return any value.

  3. Function Body : The function body is enclosed in curly braces and contains a set of statements that are executed when the function is called.

4.1.6 函数参数

在C语言中,实际参数(Actual Parameters)通常被称为 "arguments",而形式参数(Formal Parameters)则通常被称为 "parameters"。以下是它们的英文解释:

  1. Formal Parameters (形式参数):在函数定义中声明的参数,它们定义了函数的输入类型和名称。Formal parameters are the parameters declared in the function definition that specify the data types and names of the inputs the function can accept.

  2. Actual Parameters (实际参数):在函数调用中传递给函数的值或表达式,这些值将被传递给函数定义中对应的形式参数,以供函数内部使用。Actual parameters are the values or expressions passed to a function in a function call. These values are passed to the corresponding formal parameters in the function definition for use within the function.

这些术语通常在C语言中用于描述函数的输入和输出。例如,你可以说 "I passed the actual parameters '5' and '3' to the function, and they were received by the formal parameters 'a' and 'b' within the function." 这表示在函数调用中,实际参数'5'和'3'被传递给函数,然后在函数内部,它们分别被形式参数'a'和'b'接收和使用。

把实际参数传递给形式参数的时候,形式参数 parameters 有自己的独立的空间, 在传递的时候, parameters 的空间内的值改变了, 但是原来的实际参数 arguments 的并没有改变

解释: 当实参传递给形参的时候,形参是实参的一份临时拷贝,对形参对修改不能改变实参

4.1.7 小例子

我们来实践一个函数:// 交换两个变量的值

SOL 1

4.1.1 swap-two-var-none.c
// 交换两个变量的值

#include <stdio.h>

// 引入中间变量e,用于交换变量c d 的值
void Swap(int c, int d) {
    int e=0;
    e = c;
    c = d;
    d = e;
    // printf("%d %d", c, d);
}

int main(void) {
    int a = 0, b = 0;
    scanf("%d %d", &a, &b);
    printf("a=%d b=%d\n", a, b); //交换前测试

    Swap(a, b);
    printf("a=%d b=%d\n", a, b);

    return 0;

}

SOL 2

4.1.2 swap-two-var-pointer.c
// 交换两个变量的值

#include <stdio.h>

// 引入中间变量e,用于交换变量c d 的值
void SwapPointer(int *pC, int *pD) {  // 这里已经定义了指针类型参数,这里的变量是 pC、pD 


    int e=0; 
    e = *pC; // e=a
    *pC= *pD; // a=b
    *pD= e; //  
    
    // printf("%d %d", pC, pD);
}

int main(void) {
    int a = 0, b = 1;
    scanf("%d %d", &a, &b);
    printf("a=%d b=%d\n", a, b); //交换前测试

    SwapPointer(&a, &b); // 这句要这样写 还不不知道为啥? pC、pD、:运算符
    printf("a=%d b=%d\n", a, b);

    return 0;

}

图解:

最后,在 SOL 1 中并没有实现交换,在 SOL 2 中完成了实现。指针的知识会在后面提到。

5.2 节会讲到为什么 SOL 1 为什么没有实现两变量值的交换。

Last updated