Study Notes 04-Functions

Source: Internet
Author: User
I. Function Classification

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

 

1. 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)

 

2. 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 a function is declared 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.

 

3. 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

 

The declaration and definition of the sum function are placed 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

 

Q: Can I include the sum. c file in Main. C, instead of the sum. h file?

# 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.

 

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 ).

1 // B is the form parameter of the test function (form parameter) 2 void test (int B) 3 {4 B = 9; // changed the value of parameter B 5} 6 7 int main () 8 {9 int A = 10; 10 printf ("A: % d \ n before function call ", a); 11 12 test (a); // A is the real parameter (actual parameter) of the test function 13 14 printf ("A: % d", a) after the function is called ); 15 return 0; 16}

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:

 

 

 

 

 

 

 

 

 

 

Study Notes 04-Functions

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.