[C Language] 04-Functions

Source: Internet
Author: User

Note: This C language topic is a prelude to iOS development. To enable programmers with object-oriented language development experience to quickly get started with the C language. If you have no programming experience or are not interested in C and iOS development, ignore

I. Function Classification

As mentioned above, functions in C language are "methods" in object-oriented languages. C functions can be divided into three types:

1. main function, that is, main function. Each program can have only one or more primary functions. No matter where the main function is written, C Programs always start to execute the main function.

2. developer-defined functions; optional; unlimited number

3. library functions provided by C language, such as the output function printf () in stdio. h and Input Function scanf ()

 

Ii. Description and definition of functions

Although the functions in C are similar to the methods in Java, there are differences in usage.

1. in Java, there is no limit on the definition sequence of each method. You can call the method defined later within 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 row 1st can call the sum method defined in row 5th.

 

2. In the Standard C language, the order of function definition is exquisite. By default, only the function defined later can call the previously defined function.
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 in row 5th calls the sum function of row 1st, which is legal. If you change the order of the sum and main functions, it is invalid in the Standard C compiler environment (however, in Xcode, only warnings are used, and in Xcode, the GCC compiler is used)

 

3. If you want to write the definition of other functions after the main function, and the main function can call these functions normally, you must declare the function before 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 return 0; 8} 9 10 // Function Definition (Implementation) 11 int sum (int a, int B) {12 return a + B; 13}

We made the declaration of the sum function in row 2nd, and then we can call the sum function normally in row 6th (in the main function.

Function declaration format:

Return Value Type Function Name (parameter 1, parameter 2 ,...)

The parameter name can be omitted. For example, the preceding sum function declaration can be written as follows:

int sum(int, int);

As long as you declare a function before the main function, the main function will know the existence of this function and can call this function. What is the purpose of this function? We also need to look at the definition of the function. If there is only a function declaration without function definition, the program will encounter an error during the link.

 

4. in a large C program, in order to develop sub-modules, the declaration and definition (that is, implementation) of functions are usually put in two files respectively, and the function declaration is put in. in the header file h, the function definition is placed in. c source file

Next we will place the Declaration and definition of the sum function in sum. h and sum. c respectively.

Sum. h file

 

Sum. c file

 

Then, the sum function can be used to include sum. h In main. c.

In fact, the file names of sum. h and sum. c must be the same. You can write them as long as the file name is legal.

Analysis of operation steps:

1> before compilation, the pre-compiler will copy the content in the sum. h file to main. c.

2> compile the main. c and sum. c source files to generate the target files main. obj and sum. obj. These two files cannot be executed independently, for a simple reason:

* The main function does not exist in sum. obj and cannot be executed.

* Main. although the main function exists in obj, it calls a sum function in the main function, but the definition of sum function exists in sum. obj, so main. obj depends on sum. obj

3> link main. obj and sum. obj together to generate an executable file.

4> run the program

 

Here, some people may wonder: Can I include the sum. c file in main. c, instead of the sum. h file?

Everyone knows that the # include function is to copy the content, so the above Code is equivalent:

From this perspective, there is absolutely no syntax problem, but it cannot run, and an error will occur during the link. Cause: the compiler will compile all. c source file, including main. c. sum. c. Generate sum after compilation is successful. obj, main. obj file. When the two files are linked, the linker will find sum. obj and main. obj contains the definition of the sum function, so the error "repeated identifiers" is reported.

 

Some people may think that it is silly to split the sum. h and sum. c files. If there are two more files on the client end, can you write everything to main. c?
  • Yes, the code of the entire C program can be written in main. c. However, if the project is very large, you can imagine how huge the main. c file will be, which will seriously reduce the development and debugging efficiency.
  • If you want to complete a big project well, you need a team to work together, not just by yourself. If you write all the code in main. c, it leads to code conflicts, because the developers of the entire team are modifying main. in the c file, the Code modified by James is likely to erase the code added by James.
  • The normal mode should be like this: Assume that Michael is responsible for writing the main function and Mr. Li is responsible for writing a series of user-defined functions. Michael needs to use a function compiled by Mr. Li. What should I do? Li Si can declare all functions in one. in the H file, such as lisi. h. Then, Michael included lisi. then you can call lisi. h. c) Compile the function definition to implement those functions in lisi. h. In this way, Zhang and Li can collaborate with each other without conflict.

 

Iii. function parameters and real parameters

When defining a function, the variables defined in () after the function name are called formal parameters (Form parameters). The values passed in when calling the function are called actual parameters (real parameters ).

// B is the form parameter of the test function (form parameter) void test (int B)
{B = 9; // changed the value of parameter B} int main () {int a = 10; printf ("a: % d \ n before function call ", a); test (a); // a is the real parameter of the test function (actual parameter) printf ("a: % d", a); return 0 ;}

If the basic data type is used as the form parameter of the function, it is a simple value transfer. The value of real parameter a is assigned to the form parameter B, which is equivalent

int a = 10;int b = a;b = 9;

A and B are two variables with different memory addresses. Therefore, the value of parameter B is changed and the value of parameter a is not affected.

The output result of the above Code is:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.