9.1 function Overview
A self-contained unit used to complete the program code for a specific task.
2. functions make the program more modular and facilitate the reading, modification and improvement of the program.
Before writing function code, you must first consider the functions of the function and the relationship between the function and the program as a whole.
9.1.1 function example
I. starbar identifiers are used three times in different locations: the function prototype informs the compiler of the function type. function calls result in the execution of the function, the Function Definition specifies the specific function of the function.
2. functions are of the same type as variables. Before using a function, any program must declare the type of the function. After a function is added, a semicolon indicates that the statement is used to declare the function, not to define the function.
3. The starbar () prototype can be placed before main () or.
4. The program includes starbar () and main () in the same file. You can also put them in two different files. It is easier to compile a single file, while using two files facilitates the use of the same function in different programs.
5. the variable count in starbar () is a local variable. This means that the variable is only available in starbar.
9.1.2 define functions with parameters: formal parameters
You can change the upper-tree function:
Void show_n_char (char ch, int num)
{
Int count;
For (count = 0; count <WIDTH; count ++)
Putchar (ch );
}
Void show_n_char (char ch, int num): notifies the compiler show_n_char () to use two parameters named ch and num and declare their types. Ch And num are called formal parameters and formal parameters. They are private to functions as local variables.
Because the value used by the called function is copied from the called function, no matter what operation is performed on the copied value in the called function, the original value in the called function will not be affected.
You can use return to return a value from the function.
Another function of the return Statement is to terminate the execution of the function and return the control to the next statement that calls the function.
9.1.3 Function Type
Function declaration must be performed before using the function.
The function declaration only tells the compiler about the function type, while the function definition part is the actual implementation code of the function.
9.1.4 function prototype
The called function first places the parameters in a temporary storage area of the stack, and then the called function reads these parameters from the stack.
The function prototype is a powerful complement to the language. It allows the compiler to discover errors or omissions that may occur during function usage.
9.2 Recursion
C allows a function to call itself. This call process is called recursion.
Recursion can be used instead of loop statements, which makes the program structure beautiful, but its execution efficiency is not high.
9.2.1 function example
Each level of recursion uses its own private Variable n. The called address is the same as the returned address.
Each callback function call will return a result. When a program stream is executed at the end of a level-1 recursion, it is transferred to the previous level recursion for further execution, and is returned gradually through each level of recursion.
In recursive functions, the statements before recursive calls have the same execution sequence as the called functions at different levels.
In recursive functions, the statements before recursive calls have the opposite execution order as the called functions at different levels.
Although each level of recursion has its own variables, the function code will not be copied.
Recursive functions must contain statements that can terminate recursive calls.
9.2.2 Advantages and Disadvantages of Recursion
Advantage: it provides the simplest solution for some programming problems.
Disadvantage: Some recursive algorithms will soon exhaust computer memory resources.
Compilation of more than 9.3 source code file programs
9.3.1 UNIX
First, assume that the standard unix c compiler cc is installed in the UNIX system. Files file1.c and file2.c contain C functions. The following command compiles the two files and generates the executable file a. out.
Cc file1.c file2.c
Two target files, file1.o and file2.o, will be generated.
Use the following command to compile the target code of the first file and link it to the second file.
Cc file1.c file2.o
9.3.2 Linux
Gcc file1.c file2.c
Gcc file1.c file2.o
9.3.3 Windows
In Windows, compilers are engineering-oriented. The project describes the resources used by a specific program.
When running multiple files, you need to use the corresponding menu command to add the source code file to a project.
If you place the main () function in the first file and the User-Defined Function in the second file, you still need to use the function prototype in the first file.
If you place the function prototype in a header file, you do not have to input its prototype declaration every time you use these functions.
9.4 pointer Introduction
A pointer is a variable whose value is an address.
Int * ptr = & pooh; assign the pooh address to ptr. Ptr is a variable, and & pooh is a constant.
9.4.1 indirect OPERATOR :*
You can use an indirect operator to obtain the value that points to the address.
9.4.2 address OPERATOR :&
The unary operator can obtain the storage address of the variable. The address of a variable can be viewed as its location in the memory.
% P indicates the output address.