Function
1. Function definition
function return value type function name (formal parameter list)
{
function body;
}
Attention:
When defining an argument function, the definition of a formal parameter can be either traditional or modern in two ways
1) Traditional way:
int Max (x, y)
int x, y; cannot define variables other than parameters
{}
2) Modern Way:
int Max (Intx,inty)
{}
2. function return value type
Form 1:
int fun (int x, int y)
{
return expression;
}
Note: The returned value is given as the whole form of the calling function, and the expression is evaluated first and the return value
Form 2:
void fun (int x)
{
Be sure not to add a return statement;
}
Note: Void does not reserve space to store return values
3. Function call
3 Consistent: number, type, location
Mode 1: Non-void type
Variable name = function name (argument list);
Mode 2:void Type
Function name (argument list);
4. Function Prototype Declaration
Form 1: Function type function name (parameter type 1 parameter 1, parameter type 2 parameter 2 ... );
Form 2: Function type function name (parameter type 1, parameter type 2 ...);
5. Variable three attributes
Variable three attributes: type--definition, scope--space, storage category--time
1) Type
List of type name variable names;
2) Scope of action--local variables and global variables
A, local variables (or internal variables)
Definition: A variable defined inside a function
A, a local variable can only be valid within the function in which it resides
B. Variables with the same name can appear in different functions, respectively, and they belong to different variables
c, a variable defined in a compound statement can only be valid in this compound statement
D, all formal parameters are local variables
< EM id= "__mcedel" >
B, global variable (or external variable)
< EM id= "__mcedel" > define: Variables defined outside the function
A, the valid range of global variables is in this file from the definition of the position of the variable to the end of this file
b, a global variable can be referenced by each function within its valid range
C, in the same file if the global variable has the same name as the local variable, the local variable "mask" global variable
3) Storage Category
A, Auto category variable (can be omitted)
Define the same description category:
(storage Class) type name variable name
A, auto category variables after the use of free space occupied
b, local variables implicitly, when no initialization, the initial value is random values auto category
C, short use time, usually auto category variable
B, static category variable
/em> Define the same description category:
Storage category type name Variable name
A, static category from the definition to the end of the program run to occupy storage space
b, the global variable defaults to the static category, without initialization, the initial value is 0
c, static class variables are initialized only once
C, register category variables
Define the same description category:
Storage category type name Variable name
A, register category can only be a local variable can be described
b, generally not directly used
D, extern category variables
Format 1: Define the same description category:
Storage class type name variable name;
Format 2: Define, explain separately
Type name variable name;
Storage class variable name;
< EM id= "__mcedel" > /em> A, extern category variable can increase the scope of the variable
B, two description formats
6, Precompiled command
Precompiled commands
file contains (include)
#include <>--the search for a library file under the specified directory
#include ""-searches under the current working directory or sub-directory under the specified standard search path
7, macro
First type: no parameter macro definition
Format:
#define Macro Name Macro content
Function: Use a specified identifier (macro name) to represent a string of characters (macro content)
Attention:
1) Macro name we generally use uppercase letters, followed by the user-defined identifier naming rules
2) #define可以在函数外定义也可以在函数内定义, but the command should be on a separate line
#undef命令可以提亲终止宏名的作用域
3) When you make a macro definition, you can refer to the defined macro name for layer substitution
4) in the macro substitution, you must replace all the macro names before the operation, while the body of the process can not add parentheses
The original mode is replaced and then calculated by the priority level, do not add parentheses
The second type: With the parameter macro definition
Format:
#define Macro Name (parameter list) macro content
Features: Provides a more flexible way to replace
Attention:
1) When defining a macro, the argument list must be enclosed in a pair of parentheses with no spaces between the parentheses and the macro name
2) when replacing a parameter with a macro name, you need to change the parameter to the corresponding argument, and one of the corresponding relationships between the formal parameter and the argument
8, Small Knowledge Summary
1) A source program file can consist of one or more functions
function is the basic unit of C language
The main () function is called by the operating system OS.
The main () function can call other sub-functions, the child function can call other child functions, the child functions can also call themselves
2) A C language program may consist of one or more source program files
3) C program execution always starts at main () and ends at Main (); Other functions can be called
4) functions cannot be nested defined, but can be called each other, and cannot call the main () function's classification
Nested definition: You cannot define a function in a function
5) Type difference
Prototype declaration--there is a function return value type ID, with function name, parameter type, semicolon
Call function--there is a function name, parameter
Define call Function--there is function return value type identification, function name, parameter type, no semicolon
6) Three big formulas
Formula 1: expression-to see its legitimacy, to have short-circuit priority (and, or), pick operation Mr. Foo calculation of high-level, in the same case consider the combination of
Formula 2: Find the pairing, find the control, the slogan sentence
Formula 3: Encounter return or return to the function call
7, C language-function