C-function, c Function Definition
1. C's design principle is to use functions as the component module of the program.
2. A function is a self-contained unit of program code that completes a specific task. A function generally has two functions to execute some actions. A value is returned for the calling program.
3. The advantages of using functions, code reuse, and more modular programs.
4. Generally, the function is regarded as a black box, which produces specific results for certain input without considering internal implementation, unless it is the function writer. In this way, the function helps focus on the overall design of the program rather than the specific implementation details.
5. To compile a function, consider the functions required by the function and the relationship between the function and the program as a whole.
6. The function declaration (anci c function prototype) tells the compiler function type and needs to find the function definition elsewhere.
The function definition, the specific implementation of the function, specifies the specific function of the function.
The function is called, causing the function to be executed.
The function must be declared first and then used. Before calling a function, you do not need to declare it. If the function definition is called or in other files, you must declare it before calling it. The called function declaration can be placed in any position of the called function except the called function.
7. function declaration (anci c function prototype)
Return Value Type Function Name (type 1 parameter 1 parameter 2 parameter 2 parameter 2);) (declares the function name, function return value type, number and type of function parameters)
Return Value Type Function Name (parameter Type 1, parameter type 2); (parameter name omitted)
Int prinf (char *,...); // declaration of uncertain parameter functions
Void aaa (void); // declaration of a function without parameters and return values
Int bbb (int m, int n );
Int bbb (int, int );
Old function declaration:
Void aaa ();
Int bbb (); (you only need to declare the type of the function return value without parameter information)
The benefits of anci c function prototype notify the compiler of more information so that it can check more errors when compiling function call statements.
8. Function Definition
Return Value Type Function Name (type 1 parameter 1 parameter 2 parameter 2) // function Header
{
// Function body
}
Int bbb (int m, int n)
{
// Function body
}
The formal parameter is a local variable, which is private to the function, just like the variable defined in the function. Variables with the same name are used outside the function immediately, and there is no conflict between them. They are identical, independent, and irrelevant.
Old Function Definition:
Int bbb (m, n)
Int m, n;
{
// Function body
}
9. function call
Function Name (real parameter 1, real parameter 2 );
Aaa ();
Bbb (12, 5 );
The formal parameter is the variable in the called function (declared in the function header), and the actual parameter (real parameter) is the specific value that the called function passes to the called function. Transfer is a copy operation, that is, any operation performed on the copy value in the called function will not affect the original value in the called function.
When a function is called, it creates a local parameter and uses the real parameter for initialization.
10. Use the return statement to return a value from the function, terminate the execution of the function, and give the program control to the next statement that calls the function. It is generally the last statement of the function body. This value is the return value of the function and is stored in an anonymous variable. Therefore, function call statements can be used to assign values and perform operations.
If the return value type is different from the return value type defined by the function, it is automatically converted to the defined return value type.
The relationship between functions, called and called; between functions communicate with return values through parameters. All functions in C have the same status and can call other functions or be called.
11. C allows a function to call itself, which is called recursion.
Recursion principle:
Each level of function has its own private variable;
Each callback function call will return a result;
In a function, the statements before the recursive call have the same execution sequence as the called functions at all levels. The statements after the recursive call have the opposite execution sequence as the called functions at all levels;
Although each level of function has its own private variables, the function code will not be copied;
Recursive functions must contain statements that can terminate recursive calls.
Tail recursion is to put recursive calls at the end of the function, before the return statement. Tail recursion serves as a loop statement.
The disadvantage of recursion will quickly occupy a large amount of memory.
12. Multi-source code file Compilation
The main function is in a source file. The user-defined function is in a source file, and the function prototype and constant definition are placed in the header file.
13. pointer
The pointer is the memory address, which is an unsigned integer, usually in hexadecimal format.
A pointer is a variable that stores an address.
Pointer declaration:
Int * point; // pointer to an integer variable. The data type is int *. * indicates that point is a pointer variable, and int indicates that point points to an integer variable.
Char * chr; // pointer to a character variable
Float * flt; // pointer to a single-precision floating point variable
Pointer OPERATOR: Indirect operator (value operator) *, address operator (take address operator )&
& Is followed by a variable name, & the address of the variable, * is followed by a pointer variable or address, * is used to give the value stored in the directed address.
Int a = 10, B;
Int * point, * point2;
Point = &;
B = * point;
When writing a program, a variable has three attributes: the name, type, and value of the variable. After compilation and loading, the variable has two attributes: Address and value. The variable address can be seen as the name in the computer.
14. pointer as a function parameter
Passing the pointer to the called function variable as the real parameter can change the variable of the called function.
Do not pass the pointer of the called function variable as the return value to the called function, because the lifecycle of the local variable ends after being executed by the called function and is released, when this value is used to call a function, an error is returned.