Directory of this document
- First, the classification of functions
- Ii. declaration and definition of functions
- Iii. formal parameters and arguments of a function
Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore. First, the function of the classification has been said that the C language function is Object-oriented "method", C-language functions can be divided into 3 categories: 1. The main function, which is the main function. There can be only one or one main function in each program. The C program always executes from the main function, regardless of where the main function is written. 2. Developer custom functions, optional, unlimited number. The 3.C language provides library functions, such as the output function in Stdio.h, printf (), and the input function scanf (). Declaration and definition of functions although the functions in C are similar to those in Java, there are differences in usage.
1. In Java, there is no limit to the order in which each method is defined, and the method defined later can be called inside the method defined earlier.
1 public void Test () {2 int c = SUM (1, 4); 3 }4 5 public int sum (int a, int b) {6 return a + b;7 }
The test method defined in line 1th can call the sum method defined on line 5th.
2. In the standard C language, the order in which functions are defined is fastidious, and by default only functions defined by the following can be called by a function that is previously defined.
1 int sum (int a, int b) {2 return a + b;3 }4 5 int main () 6 {7 int c = SUM (1, 4); 8 return 0;9
}
The main function defined by line 5th calls the SUM function of line 1th, which is legal. If you swap the order of the SUM function and the main function, it is not legal in the standard C compiler environment (but only in Xcode, with the GCC compiler in Xcode).
3. If you want to write the definition of the other functions behind the main function, and the main function can call these functions normally, then you must make a declaration of the function in front of the main function.
1//Just make a function declaration, and do not implement 2 int sum (int a, int b); 3 4 int main () 5 {6 int c = SUM (1, 4); 7 retur n 0; 8 } 9 //function definition (Implementation) int sum (int a, int b) { return a + b;13 }
We make a declaration of the SUM function in line 2nd, and then we can call the SUM function normally in line 6th (main function). Declaration Format for functions:
return value type function name (parameter 1, parameter 2, ...)
The parameter name can be omitted, such as the SUM function declaration above can be written like this:
int sum (int, int);
As long as you declare a function before the main function, the main function knows the existence of the function and can call this function. What exactly does this function do, and what is the definition of the function? If there is only a function declaration, and there is no definition of the function, then the program will error when linking.
4. In a large C program, in order to sub-module development, the function is generally declared and defined (that is, implementation) in 2 files, the function declaration is placed in the. h header file, the function definition is placed in the. C source file.
Let's use the SUM function by placing the declaration and definition of the SUM function in sum.h and SUM.C respectively sum.h the file Sum.c file and then including MAIN.C in Sum.h In fact, sum.h and sum.c file names are not the same, can be casually written, as long as the file name is a legitimate run step analysis: 1> before compiling, the pre-compiler will copy the contents of the sum.h file into main.c. 2> then compiles the main.c and sum.c two source files, generating the target file Main.obj and Sum.obj, the 2 files cannot be executed alone, for the simple reason: * There is no main function in sum.obj, it must not be executed * Although there is a main function in Main.obj, it calls a SUM function in the main function, and the SUM function is defined in sum.obj, so Main.obj relies on sum.obj. 3> the Main.obj and sum.obj together to generate the executable file. 4> Run the program.
Here, some people may have doubts: Can you include SUM.C files in Main.c, don't sum.h files?
We all know that the function of # include is to copy content, so the above code is equivalent to: In this case, there is absolutely no problem in syntax, but it definitely does not work, and the link will be wrong. Cause: The compiler compiles all of the. C source files, including MAIN.C, SUM.C, which generates Sum.obj, Main.obj files after successful compilation, and when linking the two files, the linker discovers that both Sum.obj and main.obj have the definition of the SUM function, so the report " The identifier duplicates "error.
Some people may feel that the separation of sum.h and sum.c file of this practice silly B, good 2 more files, you put all the things are written to main.c not be OK?
- Yes, the code for the entire C program can be written in main.c. However, if the project is done very well, you can imagine how large the main.c file would be and would severely reduce the efficiency of development and commissioning.
- To do a great project well, you need a team to work with, not one person can make the set. If all the code is written in main.c, it will cause code conflicts, because the entire team of developers are modifying the Main.c file, Zhang San the modified code is likely to erase the code that was added before John Doe.
- The normal pattern should be this: assuming Zhang San is responsible for writing the main function, John Doe is responsible for writing a series of custom functions, Zhang San need to use a John Doe write a function, how to do? John Doe can declare all functions in an. h file, such as Lisi.h, and then Zhang San include the lisi.h file in his own code, then the function declared in lisi.h can be called, and John Doe, can be independently in another file (such as LISI.C) to write the definition of the function, Implement those functions declared in the lisi.h. In this way, Zhang San and John Doe can work together and do not clash.
Iii. formal parameters and arguments of a function
When defining a function, the variable defined in () after the function name is called a formal parameter (formal parameter), and the value passed in when the function is called is called the actual parameter (argument).
{ b = 9;//Change the value of parameter B}int main () { int a = ten; printf ("a:%d\n before function call", a); Test (a); A is the argument of the test function (the actual argument) printf ("a:%d after function call", a); return 0;}
If the base data type is the formal parameter of the function, it is a simple value pass, which assigns the value of argument A to parameter B, which is equivalent to
int a = 10;int b = a;b = 9;
A and B are 2 variables with different memory addresses, so changing the value of parameter B does not affect the value of argument a. The output of the above code is:
C language 04-functions