first, the use of functions
1. Each function must be defined before it is used
Example: void function (); With semicolons
int main () {...}
void Add () {...} No semicolon
2. file contains processing:
#include < file name > Find in the directory where the library function header file is located (Standard way)
#include "file name" is located in the current working directory, if not, use the standard method
3. Invocation of functions
Formal parameters: The parameter specified when defining a function
Arguments: Parameters that are specified when a function is called
Note: C language rules can only be passed to arguments by formal parameters
second, the function of the scope of the variable
1. The scope of the local variable is the entire function (including the main function) where it resides
Formal parameters can be considered as local variables
2. Scope of global variables: starting from the position where the variable is defined, to the end of the program
If the global variable has the same name as a local variable in the same source file, the global variable will be masked within the scope of the local variable.
3. Invocation of the function:
Control Flow: The order in which statements are executed
Data flow: The process by which data flows between functions when a function call occurs and ends, such as the passing of parameters, the return
Third, the implementation of the function call process
1. Memory distribution of the process
Operating system |
|
Code |
|
Data segment |
Where the compiler stores string constants |
Heap |
Memory space primarily used for dynamic applications |
Stack S1 |
When a function is called, the system automatically allocates a block in the stack space |
S2 |
Domain gives this function, which holds information such as formal parameters and local variables |
...... |
|
Global variables |
|
2. Global variables are initialized to 0
four, array and function parameters
1. Function definition: double averagee (int a[], int n) {...}
Function call: Average (score, n);
Note: Data types must be consistent
Because the shape parameter group name is essentially a pointer, it can be defined without specifying a size
Because the array name does not have length information, you need to add a parameter to pass the length of the array
v. Storage properties of Variables
1. Storage type:
Auto (automatic type) |
Default local variables, which belong to the default type |
Register (registers type) |
stored in the CPU register, advantages: High efficiency, disadvantage: Few, only for the int type |
Static (quiescent type) |
Properties: Global life, local visibility, inheritance |
extern (external type) |
is not a variable definition, just extends the scope of an external variable (scope: The variable definition position to the end of the source file, and other extern instructions) |
C Language Learning notes-7. Functions