C Language Guide (four) function chapter

Source: Internet
Author: User

In fact, in the beginning of the first program Hello World, has used the function, output printf function, encapsulates this series of operations of the main function. C Language Program is composed of a combination of functions, which is the C language modular performance. The so-called modularity is to divide complex tasks into a number of small tasks, each small task is a module, the first to implement each module, and then achieve the final goal. This modular thinking reduces the complexity of the program and the logic is clearer.

Definition of a function

return value type  function name (parameter) {    function Body}

Return value type: A function can have a return value, or there can be no return value, there is a return value need to indicate whether the type of the return value is int or another type, no return value write void.

Function Name: The name of the functions.

Parameters: can have parameters or no parameters. The parameter is a formal parameter, and the value passed to the parameter is the actual parameter.

Function Body: Contains declarations and statements.

Instance:

/* Hello () function, print Hello world! , no parameter no return value */void Hello () {    printf ("Hello world! \ n ");    return;}

/* Returns the value of the large number */int max (int a, int b) {    int c = a>b? a:b;    return c;}

  

Declaration of a function

The function declaration tells the compiler the function name and how to call the function.

Returns the value type  function name (parameter);
int max (int a, int b);

Parameter types are required in the declaration of a function, and parameter names can be omitted.

#include <stdio.h>intMaxint,int);intMain () {intMax; Max= Max (7,4) ; printf ("Max is%d \ n", Max); return 0;} intMaxintAintb) {    intc = a>b?a:b; returnC;}
Example

The function definition does not need to write a declaration before the main () function (the main function), after the main () function, or in other source files, the declaration needs to be written before main ().

intrinsic and external functions

Intrinsic functions:

A function defined in a source file can only be called by a function in that file, not a function in another file in the same program.

Static return value type function name (parameter)

For a function that only needs to be used in one source file, define it as an intrinsic function without worrying about the effect of the same name as the function in other files.

External functions:

Can be called by a function in the same source file, or by a function in another file in the same program.

extern return value type function name (parameter)

In C, the definition function does not write extern, and the default is an external function. Calling an external function requires the declaration of the function to be written first.

  

 

Call to function

Once the

function is called, the program jumps directly from the calling statement to the function's definition statement until it returns from the function.

 #include <stdio.h>extern  int  Max (int  A, int   b);  extern  void   hello ();  int   main () { int          Max; Max  = max (7 , 4       "  Max is%d \ n  "   return  0  ;}  
file.c
#include <stdio.h>externvoid  hello () {    printf ("Hello world! \ n")    ; return ;}    
hello.c
#include <stdio.h>externint max (intint  b) {     int c = a>b? a:b;     return C;}
max.c

Multiple source files need to be compiled together

C:\USERS\FLAMEN\GNU>GCC file.c hello.c max.c-o file.exec:\users\flamen\gnu>file.exe max for 7Hello World!

C Language Guide (four) function chapter

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.