C Language Functions

Source: Internet
Author: User
Tags function definition

First, the classification of functions

As already mentioned, the functions in C are the "methods" in object-oriented, and the functions of C can be divided into 3 classes:

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

Ii. declaration and definition of functions

  1. In the standard C language, the order in which functions are defined is fastidious, and by default only functions defined by the previous function can be called

int sum (intint  b) {     return a + b;}   int Main () {     int c = SUM (14);      return 0 ; }

NOTE: The main function defined in 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 just a warning in Xcode, with the GCC compiler in Xcode)

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

//just make a function declaration, and do not implement intSumintAintb); intMain () {intc = SUM (1,4); return 0; } //Definition of function (Implementation) intSumintAintb) {returnA +b;}

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 of function: 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 (intint);

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.

  3. 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 put the Declaration and definition of the SUM function in sum.h and SUM.C, respectively.

sum.h file

  

SUM.C file

  

Then include sum.h in main.c to use the SUM function

In fact, sum.h and sum.c file names are not the same, you can write casually, as long as the file name is legitimate

Run Step Analysis:

(1) Before compiling, the pre-compiler copies the contents of the sum.h file into the MAIN.C

(2) then compile main.c and sum.c two source files, generate the target file main.obj and Sum.obj, the 2 files cannot be executed alone, for simple reasons:

A) The main function does not exist in sum.obj and must not be executed

b) 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) Link main.obj and sum.obj together to generate executable files

(4) Running the program

4. Speaking of which, some people may wonder: Can you include SUM.C files in Main.c, don't sum.h files?

  

We all know that the feature of # include is to copy content, so the above code is equivalent to:

In this case, there is absolutely no problem with the syntax, but it definitely does not work, and it will be wrong when linking. 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.

Iii. formal parameters and arguments of a function

  1. 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 is the parameter of the test function (formal parameter)voidTestintb) {b=9;//changed the value of parameter B}intMain () {intA =Ten; printf ("a:%d\n before a function call", a); Test (a); //A is the argument of the test function (actual argument)printf ("a:%d after a function call", a); return 0;}

 2. If the basic 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 Ten ; int 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 Functions

Related Article

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.