"C程序设计教学Ch6:函数II及函数声明、定义和调用"

版权申诉
0 下载量 15 浏览量 更新于2024-02-25 收藏 333KB PDF 举报
Chapter 6 of the C programming course introduces more advanced concepts related to functions. In this chapter, students learn about function declarations, function definitions, and how to correctly call functions within their programs. Function declarations are statements that inform the compiler about the name and return type of a function. This allows the compiler to recognize the function when it is called in the program. For example, the declaration `double sqrt(double);` informs the compiler that there is a function named `sqrt` that returns a `double` value. When calling a function, the correct function name and arguments must be provided. For instance, `double sr2 = sqrt(2);` calls the `sqrt` function with an argument of 2 and assigns the result to the variable `sr2`. However, calling a function with the wrong arguments, such as `double sq3 = sqrt("three");`, will result in an error. Function definitions provide the actual implementation of the function. For example, the `area` function is defined as `double area(double x, double y) { return x*y; }`, which calculates the area of a rectangle given its length and width. In addition to learning about function declarations and definitions, students also explore examples of function usage in programs. For instance, the function `area` can be used to calculate the area of a rectangular shape by passing in the length and width as arguments. Overall, Chapter 6 of the C programming course builds upon the foundational knowledge of functions introduced in earlier chapters. By understanding how to declare, define, and call functions correctly, students can write more sophisticated programs that make use of reusable code and modular design.