C language foundation - (function - modular design)

Comments from friends are welcome✨✨ This chapter series is a deep thinking and summary of C language, and the content of C language will continue to be updated


foreword

"Function" is translated from English function. In fact, function means "function" and "function" in English. In essence, a function is used to complete a certain function. In this way, the concept of function is easy to understand. The so-called function name is to give the function a name. If the function is used to realize the sine operation, it is called the sine function.

Note: A function is a function. Each function is used to achieve a specific function. Function names should reflect the functionality they represent.
When designing a larger program, it is often divided into several program modules, each module includes one or more functions, and each function implements a specific function. A C program can consist of a main function and several other functions. The main function calls other functions, and other functions can also call each other. The same function can be called any number of times by one or more functions.
Explanation:
(1) A C program consists of one or more program modules, and each program module serves as a source program file. For larger programs, it is generally not desirable to put all the content in one file, but to put them in several source files, and a C program is composed of several source program files. In this way, it is convenient to write and compile separately, and improve the debugging efficiency. A source program file can be shared by multiple C programs.
(2) A source program file consists of one or more functions and other related content (such as instructions, data declarations and definitions, etc.). A source program file is a compilation unit. When compiling a program, it is compiled in units of source program files, not in units of functions.
(3) The execution of the C program starts from the main function. If other functions are called in the main function, the process returns to the main function after the call, and the operation of the entire program ends in the main function.
(4) All functions are parallel, that is, when defining functions, they are performed separately and are independent of each other. A function is not subordinate to another function, that is, functions cannot be defined nested. Functions can call each other, but the main function cannot be called. The main function is called by the operating system.
(5) From the user's point of view, there are two types of functions.
①Library functions, which are provided by the system, users do not need to define themselves, but can use them directly. It should be noted that the number and functions of library functions provided by different C language compilation systems will be somewhat different. Of course, many basic functions are common.
② User-defined functions. It is a function to solve the specific needs of users.
(6) From the perspective of the form of the function, there are two types of functions.
① No parameter function. When calling a function without parameters, the calling function does not pass data to the called function. No-argument functions are generally used to perform a specified set of operations. No-argument functions may or may not return function values, but generally most of them do not return function values.
② There are parameter functions. When calling a function, the calling function passes data to the called function through parameters when calling the called function. Generally, when the called function is executed, a function value is obtained for the calling function to use. At this time, the parameterized function should be defined as the same type as the return value.


1. How to define a function

The C language requires that all functions used in the program must be "defined first, then used".
The definition of a function should include the following contents:
(1) Specify the name of the function so that it can be called by name later.
(2) Specify the type of the function, that is, the type of the return value of the function.
(3) Specify the names and types of the parameters of the function so that data can be passed to them when the function is called. This is not required for parameterless functions.
(4) Specify what the function should do, that is, what the function does, that is, the function of the function. This is the most important and is resolved in the function body.
The library functions provided by the C compilation system are defined in advance by the compilation system, and the library files include the definitions of each function. Programmers don't need to define by themselves, they only need to use #include command to include relevant header files into this module.
The library functions only provide some of the most basic and general functions, and it is impossible to include all the functions that people use in practical applications. Programmers need to define the functions they want to use in the program but not provided by library functions.

2. How to define functions

2.1. Define a parameterless function

The general form of defining a parameterless function is

Type name function name ()
{ function body } or type name function name (void) { function body } The void in parentheses after the function name means "empty", that is, the function has no parameters. The function body consists of a declaration part and a statement part . When defining a function, use the type identifier (that is, the type name) to specify the type of the function value, that is, specify the type of the value returned by the function.









2.2. Define a function with parameters

The general form of defining a parameterized function is
type name function name (list of formal parameters)
{ function body } The function body includes a declaration part and a statement part.


2.3. Define an empty function

Empty functions are sometimes used in programming, and its form is
type name function name ()
{ }
The function body is empty. When this function is called, it does nothing and has no real effect.
In program design, several modules are often determined according to the needs, which are realized by some functions. In the first stage, only the most basic modules are designed, and some other secondary functions or icing on the cake functions will be added in succession when needed later. At the initial stage of writing a program, you can write an empty function (the function name is the actual function name used in the future) where the function is going to be expanded in the future, but these functions have not been edited yet, so use the empty function to occupy a place first, and expand later Replace it with a compiled function when program functions. In this way, the structure of the program is clear, the readability is good, and it is convenient to expand new functions in the future, and has little impact on the program structure. Empty functions are often useful in programming.

3. Call the function

The purpose of defining a function is to call this function to get the expected result. Therefore, you should be proficient in the method of calling functions and related concepts

3.1, the form of the function call

The method of calling a function is very simple, as we have seen before:
the general form of a function call is
the function name (actual parameter list).
If calling a function without parameters, the actual parameter list is optional, but the parentheses cannot be omitted. If the actual parameter list contains multiple actual parameters, separate them with commas.
According to the form and position of the function call in the program, there are the following three function call methods.

  1. Function call statement
    Treat the function call as a single statement. Such as printf("Hello World !\n"); At this time, the function is not required to return a value, but only required to complete certain operations.
  2. Function expression
    A function call appears in another expression, such as "c=max(a,b);", max(a,b) is a function call, which is a part of the assignment expression. At this time, the function is required to return a certain value to participate in the operation of the expression.
  3. Function parameters
    A function call as an argument to another function call. For example:
    m=max(a,max(b,c));
    Among them, max(b,c) is a function call, and its value is the larger , and it is used as max for another call The actual parameter. After assignment, the value of m is
    the largest of a, b, and c.
    Explanation: It is not necessary to include a semicolon when calling a function, only a semicolon is required as a function call statement. If used as a function expression or function parameter, the function call itself does not need a semicolon.

3.2. Data transfer during function call

  1. Formal parameters and actual parameters
    When calling a parameterized function , there is a data transfer relationship between the calling function and the called function. Known from the front: When defining a function, the variable name in the parentheses behind the function name is a
    formal parameter (referred to as a formal parameter ) or a virtual parameter . When calling a function in the calling function, the parameters in parentheses after the function name are called actual parameters (referred to as actual parameters ). Actual arguments can be constants, variables, or expressions.
  2. Data transfer between actual parameters and formal parameters
    In the process of calling a function, the system will transfer the value of the actual parameter to the formal parameter of the called function. In other words, a formal parameter gets a value from an actual parameter. This value is valid during the function call and can participate in the operation in the function.
    The data transfer between real and formal parameters in the process of calling a function is called virtual-real combination .
    Explanation:
    (1) Actual parameters can be constants, variables or expressions, for example: max(3, a+b), but they are required to have definite values. Assigns the value of the actual parameter to the formal parameter at call time.
    (2) The types of actual and formal parameters should be the same or compatible in assignment.

3.3. The process of function calling

(1) The formal parameters specified in the definition function do not occupy the storage unit in the memory when there is no function call. When a function call occurs, the formal parameters of the function are temporarily allocated memory units.
(2) Pass the value of the actual parameter to the corresponding formal parameter.
(3) During the execution of the function, since the formal parameter already has a value, you can use the formal parameter to perform related operations.
(4) Bring the function value back to the calling function through the return statement.
(5) The call ends, and the formal parameter unit is released.

3.4, the return value of the function

Usually, it is hoped that the calling function can obtain a definite value through function calling, which is the function value (the return value of the function). The
function value is described below.
(1) The return value of a function is obtained through the return statement in the function. The return statement brings a certain value from the called function back to the calling function. If you need to bring back a function value from the called function (for use by the calling function), the called function must contain a return statement. If you don't need to return the function value from the called function, you don't need the return statement.
There can be more than one return statement in a function, and the return statement will take effect when the return statement is executed. The parentheses behind the return statement are optional, such as return z; is equivalent to return(z); The value after return can be an expression.

(2) The type of the function value. Since the function has a return value, this value should of course belong to a certain type, and the type of the function value should be specified when the function is defined.
(3) The function type specified when defining a function should generally be consistent with the expression type in the return statement. If the type of the function value is inconsistent with the value of the expression in the return statement, the function type shall prevail. For numeric data, type conversion can be performed automatically. That is, the function type determines the type of the return value.
(4) For a function without a return value, it should be defined as void type (or empty type ). In this way, the system guarantees that the function will not return any value, that is, it is forbidden to use the return value of the called function in the calling function. A return statement must not appear in the function body at this time.

Fourth, the declaration of the called function and the function prototype

To call another function (namely the called function) in a function needs to meet the following conditions:
(1) The first called function must be a defined function (a library function or a user-defined function). But this condition alone is not enough.
(2) If a library function is used, the information required for calling the library function should be included .
(3) If you use a function defined by the user, and the position of the function is behind the function that calls it (that is, the calling function) (in the same file), you should declare ( declaration). The function of the statement is to inform the compilation system of information such as the function name, the number of function parameters, and the parameter types, so that when encountering a function call, the compilation system can correctly identify the function and check whether the call is legal.
The function declaration and the function header in the function definition are basically the same, except for a semicolon (the function declaration has one more semicolon than the first line in the function definition). Therefore, when writing a function declaration, you can simply copy the first line of the defined function, add a semicolon, and it becomes a declaration . The first line of a function (i.e. the function header) is called the function prototype . Why use the header of the function as a function declaration? This is to facilitate the checking of the legality of the function call. Because the first part of the function contains the basic information for checking whether the calling function is legal (it includes the function name, function value type, number of parameters, parameter type and parameter order), the function name, function type, and parameter are required when checking the function call. The number and order of parameters must be consistent with the function declaration, and the actual parameter type must be the same as the formal parameter type in the function declaration (or assignment compatibility, such as real data can be passed to integer formal parameters, and the type conversion is performed according to the assignment rules) otherwise, press Mishandled. In this way, the correct call of the function can be guaranteed.
illustrate:The use of function prototypes for declarations is an important feature of C. Using function prototypes to declare functions can reduce possible errors when writing programs. Since the position of the function declaration is relatively close to the position of the function call statement, it is convenient to refer to the function prototype to write the function call when writing the program, and it is not easy to make mistakes.
In fact, the formal parameter name in the function declaration can be omitted, but only the type of the formal parameter, such as the above statement can be written as
float add(float, float); //Do not write the parameter name, only write the parameter type
Compilation The system only cares about and checks the number and type of parameters, but not the parameter name, because when calling a function, it is only required to ensure that the type of the actual parameter is consistent with the type of the formal parameter, regardless of what the name of the formal parameter is. Therefore, in the function declaration, the formal parameter name can be written or not, and it does not matter what the formal parameter name is, such as:
float add(float a, float b); //The parameter name does not use x, y, but a, b. Legal
According to the above introduction, there are two general forms of function declarations, which are
(1) function type function name (parameter type 1 parameter name 1 , parameter type 2 parameter name 2 , ... , parameter type n parameter name n )
(2 ) function type function name (parameter type 1 , parameter type 2 , ... , parameter type n parameter name n )

Note: Definition and declaration of a function are not the same thing. The definition of a function refers to the establishment of the function of the function, including specifying the function name, function value type, formal parameters and their types, and the function body, etc. It is a complete and independent function unit. The function of the declaration of the function is to inform the compilation system of the name of the function, the function type, and the type, number, and order of the formal parameters, so that the system can perform a comparison check based on this when the function is called (for example, whether the function name is correct, the implementation Whether the type and number of participating formal parameters are consistent), it does not contain the function body. If the functions called in this file have been declared at the beginning of the file (before all functions), it is not necessary to declare the functions called in each function. Since the function to be called has been declared at the beginning of the file (outside the function) (these are called external declarations ), when the program is compiled, the compiling system has already known the relevant information of the function from the external declaration, so There is no need to repeat the declaration in the calling function. External declarations written in front of all functions are valid at the scope of the entire file.

5. Nested call of function

The function definitions of C language are parallel and independent to each other. That is to say, when defining a function, another function cannot be defined within a function, that is, nested definitions cannot be nested, but functions can be nested, that is, when calling a function In the process, another function is called.

5.1. Recursive call of function

In the process of calling a function, the function itself is called directly or indirectly, which is called the recursive call of the function . One of the characteristics of the C language is that it allows recursive calls of functions.

6. Summary

When the program has many functions and the scale is relatively large, writing all the program codes in a main function (main function) will make the main function complicated and unclear, making it difficult to read and maintain the program. In addition, sometimes a certain function (such as printing the header of each page) needs to be implemented multiple times in the program, and the program code for this function needs to be written repeatedly, which makes the program lengthy and unrefined.
Therefore, people naturally think of using the method of assembly to simplify the process of program design. Just like assembling a computer, various components (such as power supply, motherboard, optical drive, fan, etc.) are produced in advance. When the computer is finally assembled, whatever is used is taken out of the warehouse and installed directly. We will never adopt the handicraft method, temporarily produce a power supply when the power supply is used, and temporarily produce a motherboard when the motherboard is used. This is the idea of ​​modular programming.

Guess you like

Origin blog.csdn.net/weixin_44759598/article/details/128720947