Scattered knowledge points
- modularity : The process of decomposing a problem into several sub-problems becomes modular.
- the advantages of modularity : Not only can a complex problem be decomposed into several relatively simple problems, but also improve the reusability of the program code.
- function : A function is the basic unit that forms a C program. The function consists of the first part of the function and the two parts of the body, the first part of which contains the function's return type, the function name, and the parameter table declaration, and the function body contains the sequence of statements required to implement a particular function.
- function Invocation principle : The C language stipulates that all functions must be "first defined and then used". That is, if the called function is defined earlier, it can be called directly; otherwise, the function prototype of the called function needs to be declared before, in order to advertise the C compiler, the complete definition of the function is behind.
- Common standard function header files
-
- math.h--contains math-related functions
- ctype.h--contains functions related to character processing
- string.h--contains functions related to string processing
- stdio.h--contains functions related to input and output
- stdlib.h--contains functions related to dynamically allocating storage space and generating random values
- Basic format for custom functions
< function return type > < function name > (< parameter table >)
{
< function body >;
}
Description
- < function return type > refers to the data type to which the function return value belongs after the function has finished executing;
- < The name of the function name > should conform to the rules of user-defined identifiers in C, and it is best to "see the name", preferably with a ;
- < parameter table > indicates the number of arguments that need to be supplied to the function and the data type to which each parameter belongs when calling the function, which is the interface for exchanging information between functions; .
- after calling the function, the parameter passes through two basic steps: 1) allocates storage space for each form parameter according to the formal parameter declaration format. 2) then assign the value of the actual parameter to the corresponding formal parameter.
- The process of passing an actual parameter to a formal parameter has the following basic characteristics: a) having one-way, the actual parameter can be either a variable or an expression; b) After the value of the actual parameter is assigned to the formal parameter, There is no longer any relationship between the actual parameter and the formal parameter.
-
- < function body > A sequence of statements consisting of statements that need to be executed, is the core part of the function.
-
The basic format of the function call statement is:< functions > (< actual parameter table >);
- The < function name > is the one that needs to be called;
- The parameters in the < actual parameter table > correspond to the formal parameter table of this function;
- Example:
1 intdataarray[ -];2 intN;3 DoubleAve;4 5 //Custom Functions6 DoubleAverageintValue[],intnum) {7 intI, sum =0;8 for(i =0; i < num; i++){9Sum + =Value[i];Ten returnsum*1.0/num; One } A } - - //function Call theAve = Average (DataArray, n);
Application examples of custom functions
- Output multiplication Table
1#include <stdio.h>2 voidDrawLineintNCharch); Must exist3 4 Main ()5 {6 intI, J;7printf"\ 9*9 table\n");8DrawLine $,'=');9printf"\ n 1 2 3 4 5 6 7 8 9");TenDrawLine $,'='); This must be a single quote ' ', or it will have garbled characters One for(i =1; I <=9; i++){ Aprintf"\n%4d", i); - for(j =1; J <=9; J + +){ -printf"%4d", i*j); the } - if(I <9) -DrawLine $,'_'); - Else +DrawLine $,'='); - } + } A at voidDrawLineintNCharch) { - inti; -Putchar ('\ n'); - for(i =1; I <= N; i++){ - Putchar (CH); - } in}
C Language Programming Case Tutorial (2nd edition) code note (ii)