function
-
Essentially speaking, the function is used to complete certain functions.
-
Function is the function, each function is used to achieve a particular function, the function name should reflect the features of their representatives.
-
In programming function to make good use, in order to reduce the workload of repeat programming segment.
-
All functions are parallel, i.e., when functions are defined, respectively, and are independent of each other.
-
Between functions can call each other, but can not call the main function, the main function is called by the operating system.
See the perspective of users, has two functions:
-
Library functions provided by the system
-
User-defined functions, user functions to address special needs
For C compiler library functions provided by the system, are defined in advance by the compiler system, just use the # include instruction related to this file contains the header module.
Defined functions
All functions must "define, after use"
Defined functions should include the following elements:
-
Specify the name of the function
-
It specifies the type of the function, i.e., the function returns the type of value
-
Parameter name and type of the specified function
-
Function Function
The method defined functions
1, no defined function parameter
Type the name of the function name () { function body } or type name function name (void) { function body } function comprises a body portion and a declaration statement section void space expressed, i.e., no parameters
2, there is defined a function parameter
Type the name of the function name (parameter list column form) { function body } example: int max (int X, Y int) { int Z; Z = X> Y X: Y;? Return (Z); }
3, the definition of empty function
Type the name of the function name () { } For example: void dummy () { }
Call functions
First, the function call form
Function name (arguments listed)
If the argument table column containing more arguments, among the parameters are separated by commas.
The form and position of the function calls in the program appear to be divided into the following three types:
-
Function call statement
The function call as a single sentence, for example: printf_star ();
-
Function expression
Function calls appear in another expression, for example:
c = max (a,b);
-
Function Arguments
Function call argument when calling another function as, for example:
m = max (a,max ( a , b ) );
Second, the data transfer when the function call
-
Formal and actual parameters
When the function call has parameters, there are calls between the calling function and the data transfer function relationship.
When defining the function, the function name in brackets behind variable called: "formal parameters" (referred to as parameter), or "virtual function."
When a function is called in the calling function, the function name in parentheses following parameters are called: "actual parameter" (the argument), the actual parameters may be constant, variable or expression.
-
Data transfer between the arguments and parameters
In the process of the calling function, the argument value of the system will be passed to the called function parameter. Or
Value obtained from a parameter argument.
Function call occurs during the real parameter participating in the data transfer between the called: "actual situation."
-
Return value of the function by the function obtained return statement
return z; and return (z); is equivalent to
-
If not a value type and the return statement in the function value of the expression, function places subject type, determines the type of function is the type of return value.
-
For functions not back value, it shall be defined function "void type" (or void type)
Third, to the called function declarations and function prototypes
Calling another function (i.e. function calls) in a function requires the following:
-
First, the function is invoked must already defined functions (library functions or user-defined)
-
If you use a library function, you should use # include directive to call up information on the time required for library functions used in the beginning of this document.
-
If you use a user-defined function, the function of the position in the back of the calling function (ie the calling function) is (the same file), it should make a statement in the calling function to the called function
Declaratory: the name of the function, a function of the number of parameters and parameter types and other information compiled notification system
-
Compile and inspection systems only care about the number of parameters and parameter types
The general form of function declaration in two ways:
1, function type function name (parameter type 1, type parameter parameter name 1 ... n parameter name n); 2, function type function name (parameter type 1, 2 .... Parameter Type Parameter Type n)
Nested calls and recursive function calls
First, the nested call
-
In the C language, is defined functions mutually independent, parallel, i.e., by defining a function, a function can not be defined within another function, i.e., the definition can not be nested;
-
However, function calls can be nested, i.e., in the process of calling a function, and another function call;
Second, the recursive call
In the process of calling a function call appeared directly or indirectly, the function itself is called a recursive function calls.
Array as a function of the parameters
First, the array element as a function of the argument
-
Array elements as a function argument, but not as parameter. (Because the parameter is temporarily allocated storage unit when the function is invoked);
-
When using the array mode as a function argument, the argument values passed to a parameter, the "transmission value" in
-
The direction of data transfer is transmitted from the parameter argument, one-way transmission
Second, a one-dimensional array of function parameters masterpiece
When used as argument array element, transmitted to the parameter value of the variable is an array element, while an array masterpiece function arguments, to the transmission parameter (or array name pointer variable) is the address of the first element of the array
Third, multi-dimensional arrays masterpieces function parameters
Multi-dimensional arrays may be used as an argument name and a function parameter, you can specify the size of each dimension of the shape parameters defined in the called function, the size of the first dimension description may be omitted;
For example: int Array [. 3] [10]; and int array [] [10] both legitimate and equivalent to, but not the 2-dimensional size, and other high-dimensional description thereof will be omitted
In memory, the arrays are stored by rows, so when defining the two-dimensional array, you must specify the number of columns
Local and global variables
Each variable has a scope issue
Defined variables can have 3 cases:
-
Defined the beginning of the function; - local variables
-
Statements defined within the interior of the composite function; - local variables
-
External function definition; - Global Variables
Local variables
-
Variables inside a function defined only function effectively within the scope of the present;
-
Variable defined in the compound statement is valid only within the scope of the present compound statement, only references to them in the present compound statement; the compound statement is outside these variables can not be used
These are called local variables
Description:
-
Variables defined main function only effective in the main function, the main function can not use the functions defined in the other variables
-
Different functions may be used in the variables of the same name
-
Formal parameter is a local variable
-
In an internal function, the compound statement can define a variable, these variables can only be effective in the present compound statement, the compound statement also called "sub-program" or "block"
Global Variables
-
In addition to variables defined function called external variables, external variables are global variables (also known as global variables)
-
Its effective range is defined from a variable position start to the end source file;
-
Note: Variables defined within a local function variables, variables defined outside the function is a global variable
-
Set of global variables is to increase the channels of contact between function data