C language programming study notes-function introduction,

Source: Internet
Author: User

C language programming study notes-function introduction,

C language programming learning notes-function introduction.

7.1 why use a function

? If you write all the code in the main function, the program becomes complex and unclear.

? A function is a function. No function is used to implement a specific function. The function name reflects its functions.

My understanding: the F (x) function in mathematics brings variable parameters x and y into the formula for computation.

Example:

// Function

# Include

Void printf_start () // void has no type and function value. The value is not returned to the main function after the function is executed.

{

Printf ("*********** \ n ");

}

Void printf_word ()

{

Printf ("how areyou \ n ");

}

Int main ()

{

Void printf_start ();

Void printf_word ();

Void printf_start ();

Printf_start ();

Printf_word ();

Printf_start ();

Getchar ();

Return 0;

}

Note:

(1) The source file program consists of one or more functions, commands, data declarations, and definitions. A source file is a compilation unit. During program compilation, the source file is compiled in the unit of the original question, rather than the function.

(2) C program execution starts from the main function

(3) functions can be called each other but cannot be defined in nesting mode. The Main function cannot be called. The main function is called by the operating system.

From the user's perspective, functions are divided

? Library functions are provided by the system, which are not required by users

? User-Defined Functions

(4) functions are divided

? No parameter function. The main function does not pass parameters when no parameter function is called.

? When the main function is called, data is transmitted to the called function through parameters.

7.2 introduce the definition of a function for Calculation

1. Define a function: the function must be "defined first and used later"

The definition function should include the following:

? Function Name

? Function type, that is, function return value type c. function parameter name and type d. Function

2. Define the Function Method

? Define a parameter-free function

Type name function name () type name function name (void)

{{

Function body

} Void indicates "null", that is, no Parameter

? Define a function with Parameters

Type name function name (parameter table column) int max (intx, inty)

{{

Int z;

Z = x> y? X: y;

Printf ("the number z is % d \ n", z );

Return z; // return the value of z to the main function as the return value of the function.

}}

? Define empty Functions

Type name function name () void max ()

{}{}

Empty functions take a place and are not well written yet. They can be expanded later.

7.3 call a function

1. function call form

Function Name (real parameter table) // if no parameter function is called, brackets cannot be omitted.

(1) function call statement printf_start ();

(2) function expression z = 2 * max (a, B)

(3) function parameter m = max (a, max (a, B) the value of the function call is used as the real parameter of another call.

2. Data Transmission of function calls

(1) formal parameters and actual parameters

? Formal parameter: variable name in brackets during definition

? Actual parameters: parameters in brackets (can be constants, variables, or expressions)

During the function call process, the form parameter obtains a value from the real parameter, which is valid during the call and participates in the operation of the function.

(2) function call Process

? When defining a parameter, the parameter does not occupy the storage unit in the memory. During the call, the memory unit is allocated temporarily.

? The return Statement brings the return value of the function back to the main function. The return value type is the same as the function type.

? When the call ends, the parameter is released, and the real parameter is retained and the original value is maintained. One-way transmission, which can only be passed to the form parameter.

(3) return values of functions

A. the return value of the function is obtained through the return statement.

Max (int x, int y)

{

Return (x> y? X: y );

}

B. The function type determines the return value type.

C. If the type of the function return value is different from that of the specified function, follow the replication rules.

7.4 description and prototype of the called Function

2. function prototype

(1) function name of the function type (parameter type 1 parameter name 1, parameter type 2 parameter name ··········)

(2) function name of the function type (parameter Type 1, parameter type 2...

7.5 nested Functions

Functions cannot be nested, but can be nested.

7.6 recursive call of functions

7.7 array as function parameter

(1) array elements can be used as real parameters rather than form parameters.

Cause: When a function is called, a storage unit is temporarily allocated. It is impossible to assign a storage unit to an array element separately. An array is a whole and occupies a continuous storage unit in the memory.

(2) array name as function parameter

? When array elements are used as arguments, the values of array elements are passed to the parameter variables.

? When the array name is used as the real parameter of the function, the address of the first element of the array is transmitted to the form parameter (array name or pointer variable.

7. 8. Local variables and global variables

Local variable: The variable defined in the function. The variable defined in the compound statement is valid only within the statement range.

Global variable: a variable defined outside the function. The effective range is from the position of the defined variable to the end of the source file.

7.9 storage and storage of Variables

7.10 declaration and definition of variables

(1) differences:

Need to create a bucket -- Definition

No need to create a bucket -- Declaration

(2) Details

In the deep analysis of C language, I have a detailed explanation of the definition and declaration of variables. Here I will explain my distinction between definition and declaration:

Example: Inti;

Extern int I;

1. Definition:

It is to name a created object and allocate a memory space for it. In addition, an object can only be defined once in a region and cannot be defined multiple times.

2. The declaration is to create an object,

A. matching a memory space for it in advance is equivalent to a reservation. I don't have to use it now, but it's mine and you can't use it.

B. Declare it in advance, but do not use it. You can define it later when necessary.

Therefore, the difference between the Declaration and definition is that the definition has allocated the memory space, and the declaration is only pre-booked but not allocated. It will be fine to define the allocation when necessary.

7.11 internal and external functions

1. Internal functions:

A function can only be called by other functions in this file. It is called an internal function.

Static type name function name (parameter table );

For example:

Static int fun1 (int a, int B );

Fun1 is an internal function and cannot be called by other files.

Internal functions can also be static functions because they are declared using static functions.

Function: different files have internal functions with the same name, so they do not interfere with each other and shield them from the outside world.

2. External functions:

A function can be used by other files to become an external function.

Example: Extern int fun (int a, int B );

The C language specifies that extern is omitted during definition. The default value is an external function.

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.