Chapter 6 Functions
The function is the basic module unit in C. A function is designed to perform a specific function, which usually reflects its function. A function contains an explanatory and statement. This chapter describes how to explain, define and call functions. Other discussion topics are:
* Function overview
* Function definition
* Function prototype
* Function call
Function overview
The function must have a definition and one instruction. If the description is before the function is called, a definition can be used as a description, and the function definition includes the code that the function is executed when the function is called.
A function description establishs the name, return value, and the properties of the function defined in the otherwise demonstrated. A function description must be in front of the function call. This is why the reason why the header file containing the run function before you call a run function before you call a run function. If the description has information with the number of types and parameters, the description is the prototype.
For more information, see "Function Protocol" behind this chapter.
The compiler uses this prototype to compare the parameters in the rear call with the function of the function parameters, and convert the parameter type when necessary.
A function call will perform control from the call function to the called function. If there is a parameter, the parameters are passed through the value of the called function. Perform a RETURN statement in the called function returns control and a possible value to the call function.
Function description and definition format
Old Functions Description and Definition Use the syntax that is averaged in the instruction parameters than the recommendation of the ANSI C standard. First, the old-fashioned explanation does not have a parameter table; second, the parameters are listed in the function definition, but they do not explain their definitions in the parameter table. Type Description A front of a composite statement that makes up the function. The old grammar has been abolished, not used in the new code. But still supports code using old grammar. The following example gives the abolished format of the exemplified format:
Double Old_Style (); / * Function Description * /
Double Alt_Style (A, REAL) / * Overwhelming Function Description * /
Double * real;
INT A; {RETURN (* REAL A);
Returns an integer or a function of a pointer with an int with the same size does not need to have a description, although it is recommended to have this instruction.
In order to comply with the ANSI C standard, the old function explains the use of an omitted manner, now generates an error when using the / zA option; generating a 4th layer warning when using the / ze option.
E.g:
Void functl (a, ...) / * produces a warning at / ZE * /
INT A; / * or generate an error in / za * /
{
}
You can rewrite this description as a prototype:
Void Funct1 (int A, ...)
{
}
If you explain or define the same function later with a type parameter different from its lifting type, the old function will also generate a warning.
The next section "Function Definition" illustrates the syntax defined by the function, including the old grammar. The non-terminator of the parameter table in the old grammar is the identifier meter.
Function definition
A function indicates the name of the function, which accepts the parameter type and number and its return value. A function definition also includes means of a local variable and a function body that determines what the function is doing.
grammar
Conversion unit:
External description
External Description of Conversion Unit
External Description: / * Allow only within the outside (file) * /
Function definition
Description
Function Definition: / * The specifier here is a function specifier * /
Description Indicator OPT Property Sequence OPT Description Description OPT Composite Statement
/ * Property sequence is Microsoft Special Office * /
The prototype parameters are:
Description Indicator:
Storage class indicator Description Of OPT Type Indicator OPT Type Indicator Description Opt
Type modifier description indicator OPT
Description Table:
Description
Description table description
Reference:
Pointer OPT direct specifier
Direct explanatory: / * A function specifier * /
Direct specifier (parameter type table) / * new specifier * /
Direct explanatory (Identifier OPT) / * Abolished explanation * /
In a definition parameter table uses the following syntax:
Parameter type: / * Parameter table * /
Parameters Table
Parameters Table,
...Parameters Table:
Parameter Description
Parameter table, parameter description
Parameter Description:
Parameter indicator
Description Indicator Abstract Indicator OPT
Use the parameter table in an old function definition using the following syntax:
Identifier Form: / * Disabled Function Definition and Description * /
Identifier
Identifier table, identifier
Syntax of the function body:
Composite statement: / * Function * /
{Description Table OPT statement table OPT}
The storage class indicator that can be used to modify one function is only extern and static. The extern indicator indicates that the function can be referenced from other files, that is, the function name is output to the linker. The STATIC indicator indicates that the function cannot be referenced from other files, that is, the function name does not output to the linker. If a storage class does not appear in a function definition, it is assumed to be extern. In any case, the function is visible from the definition point to the file. An optional description and mandatory specifier indicates the return type and name of the function. The specifier is a combination of identifiers named the function and a combination of parentheses keeping with a function name. The transmitted attribute sequence non-terminator is a special feature defined in the "Function Properties".
Direct specifier (in the description syntax) indicates the name of the defined function and the identifier of its parameters. If the direct specifier includes a parameter table, the table indicates the type of all parameters. Such a specifier is also called a function prototype of the function later.
One instruction in the description table in the function definition cannot contain a storage class indicator other than the Register. The type indicator in the indicator syntax is omitted if the REGISTER storage class is specified for an INT type value.
The composite statement is a function body containing a local variable description, reference external instructions and statements. "Function Properties", "Storage Class", "Return Venue", "Parameters", and "Function" and other sections describe the components defined by the function.
Function properties
Microsoft Special Office
Optional attribute sequence Non-ending allows you to select a call agree on every function. You can also specify the function as __fastcall or _ _inline.
Microsoft End
Specify call conventions
Microsoft Special Office
For information on calling the agreement, see "Calling the Aggregate Topics" in the online "Microsoft Visual C 6.0 Programmer Guide".
Microsoft End
Bocardary function
Microsoft Special Office
__INline keyword tells the compiler to replace the code with the definition of this function in each instance of a function call. But replacement only appears in the decision of the compiler. For example, if the address of a function is occupied or it is too large, the compiler does not connect the function.
A function of a candidate that is considered to be a wedge, it must be defined using the new function.
Use this format to point out a connection function:
__INLINE Type OPT function definition;
Use the connection function to generate faster code, sometimes generate smaller code than the same function call, whose reasons are as follows:
* It saves the time required to perform function calls.
* Small coupled functions, may be three rows or less, because the compiler does not generate the code of the processing parameter and the return value to generate less code than the same function calls less code.
* The generated wedding function is an optimized code that cannot be used properly because the compiler does not perform the optimization of the internal process.
Use __inline's function should not be confused with the coupler, and see "Boarding Compilation" for more information.
Microsoft End
Board assembler
Microsoft Special Office
The networking assembler allows you to directly embed the assembly language instructions directly in your C source program without additional assembly and link steps.
Board assembler is built in the compiler, you do not need a separate assembler, such as Microsoft Macro Edge (MASM).
Because the joint editing module does not require a separate assembly and link step, it is more convenient than a separate assembler. Board assembly code can use any C variable or function name, so it is easy to integrate with the C code of your program. Also because the assembly code can be mixed with the C statement, it can complete the trouble or impossible functions of the use of C separately.
__ASM Keyword Adjustment Connection Connector, which can appear in any legal C statement, but cannot appear in its own statement. You must follow a compilation command, a instruction group or at least one empty parentheses enclosed with a curly brace.
The term "__ASM block" is hereby referring to any instruction or instruction group, whether or not it appears in the currency.
The following code is a simple __asm block enclosed in a curly brace (this code is an air function):
__ASM
{Push EBP MOV EBP, ESP SUB ESP, __LOCAL_SIZE}
Alternatively, you can place an __ASM in front of each assembly instruction:
__ASM Push EBP
__ASM MOV EBP, ESP
__ASM SUB ESP, __LOCAL_SIZE
Since the __asm keyword is a statement separator, you can also put the assembly instruction in the same line: __ASM PUSH EBP __ASM MOV EBP, ESP __ASM SUB ESP, __local_sizemicrosoft End
DLL input and output functions
Microsoft Special Office
Dllimport and DLlexPort Storage Class modifiers are C language Microsoft Special Displays. These modifications explicitly define DLL client interfaces (executable files or additional DLLs). Explain that the function of DLLEXPORT eliminates the need for a module definition (.dll) file. You can use DLLIMPORT and DLLEXPORT modifiers for data and objects.
Dllimport and DLLEXPORT storage class modifiers must be used with the extended property syntax keyword __declspec, the following example:
#define dllimport __declspec (dllimport)
#define dllexport __declspec (dllexport)
DLLEXPORT VOID FUNC ();
DLLEXPORT INT I = 10;
DLLEXPORT INT J;
DLLEXPORT INT N;
For specified information about the grammar of the extended storage modifier, see "Expanded Storage Type Properties" in Chapter 3, "Description and Types".
Microsoft End
Definition and description
Microsoft Special Office
The DLL interface refers to all known items (functions and data) output in a program in a system; that is, all instructions for DLLIMPORT or DLLEXPORT. All instructions included in the DLL interface must be specified as DLLIMPORT or DLLEXPORT properties. But this definition can only specify the DLlexPort property. For example, the following function definition generates a compiler error:
#define dllimport __declspec (dllimport)
#define dllexport __declspec (dllexport)
DLLIMPORT INT FUNC () / * Error: DllImport * / in definition
{RETURN 1;}
The following code also produces an error:
#define dllimport __declspec (dllimport)
#define dllexport __declspec (dllexport)
Dllimport Int i = 10; / * Error: This is a definition * /
But the following is the correct syntax:
#define dllimport __declspec (dllimport)
#define dllexport __declspec (dllexport)
DLLEXPORT INT i = 10; / * Correct: This is an output definition * /
DLLEXPORT uses implies a definition, and DLLIMPORT implies a description. You must enforce DLLEXPORT to enforce an explanation; otherwise, implicit is a definition.
#define dllimport __declspec (dllimport)
#define dllexport __declspec (dllexport)
Extern DLLIMPORT INT K; / * This is correct and implies a description * /
DLLIMPORT INT J;
Microsoft End
Define the wocker function with DLLEXPORT and DLLIMPORT
Microsoft Special Office
You can define a connection function with a DLlexPort property, in which case, the function is always instantiated and output, regardless of any module in the program references the function. This function is assumed to be entered by another program.
You can also use the DLLIMPORT attribute to indicate a function as a wocker function, in which case, the function can be extended (depending on / ob (coupled) compiler option specification but cannot be instantiated. In special circumstances, if the address of a wedding-entered function is occupied, the address of the function remains in the returned DLL. This behavior is the same as the address of a function occupying a non-wip-entered input. Static partial data and strings in the coupled function maintain the same identifier between the DLL and the customer like a single program (that is, a executable without a DLL interface).
Be careful when performing an input of the entered wedding function, for example, if you modify the DLL, do not assume that the customer uses the DLL change version. To ensure that you load the appropriate DLL version, re-establish the DLL customer. Microsoft End
Rules and Limits of Dllimport / DLlexPort
Microsoft Special Office
* If you explain that a function does not have a DLLIMPORT or DLLEXPORT property, the function does not think that the DLL interface is part. Therefore, the definition of this function must appear in another module in the module or the same program. In order to make the function become a DLL interface portion, the definition of the function must be explained in DLLEXPORT in other modules; otherwise, a linker error is generated when a customer is established.
* If your program's single module contains the DLLIMPORT and DLLEXPORT description of the same function, the priority of the DLlexPort property is high than the DLLIMPORT attribute. But the compiler produces a warning. E.g:
#define dllimport __declspec (dllimport)
#define dllexport __declspec (dllexport)
DLLIMPORT VOID FUNC1 (VOID); DLLEXPORT VOID FUNC1 (VOID); / * Warning: DLLEXPORT is more preferred * /
* You cannot initialize a static data pointer with an address of the data object illustrated by the DLLIMPORT property.
For example, the following code generates an error: #define dllimport __declspec (dllimport) #define dllexport __declspec (dllexport) DLLIMPORT INT i ;. .. Int * pi = & i; / * error * / void func2 () {static int * pi = & I; / * Error * /}
* Initialize a static function pointer with an address of a DLLIMPORT description, set the pointer to the DLL input pattern (a conversion control to the code block of the function) instead of the address of the function. The following assignment does not generate an error message:
#define dllimport __declspec (dllimport)
#define dllexport __declspec (dllexport)
DLLIMPORT VOID FUNC1 (VOID)
static void (* pf) (void) = & func1; / * No error * /
Void func2 ()
{
Static void (* pf) (void) = & func1; / * No error * /
}
* Because programs including the DLLEXPORT property in an object must provide the definition of this object, you can initialize a global or partial static function pointer with an address of a DLlexPort function. Similarly, you can initialize a global or partial static data pointer with an address of a DLLEXPORT data object. E.g:
#define dllimport __declspec (dllimport)
#define dllexport __declspec (dllexport)
DLLIMPORT VOID FUNC1 (VOID);
DLLIMPORT INT I;
DLLEXPORT VOID FUNC1 (VOID);
DLLEXPORT INT I;
.
INT * PI = & i; / * correct * /
Static void (* pf) (void) = & func1;
/ * Correct * /
Void func2 () {static int * pi = i; / * correct * / static void (* pf) = & func1; / * correct * /}
Microsoft End
Naked function
Microsoft Special Office
Naked storage class properties is a Microsoft special disposal of the C language. For functions described with Naked store class properties, the compiler generates a code that is not preamble and ending. You can take advantage of this feature that is especially useful in using the Co-editing virtual device driver. Because the naked property is only related to the definition of a function, it is not a type modifier, named
The function uses the extended attribute syntax described in "Excounted Storage Class Properties" in Chapter 3, "Description and Type".
The following example defines a function with the naked attribute:
__DECLSPEC (Naked) int func (formal_parameters)
{
/ * Functional body * /
}
Alternatively, another way is:
#define naked __declspec (naked)
Naked Int Func (Formal_Parameters)
{
/ * Functional body * /
}
Naked Attributes only affects the compiler to generate the priority of the function and the code end sequence, it does not affect the code generated by the call. Therefore, the NakeD attribute does not think that the type of the function, the function pointer does not have Naked properties. And the Naked property cannot be applied to a data definition. For example, the following code generates an error:
__DECLSPEC (Naked) INT i; / * Error -Naked Property is not allowed in the data description * / naked attribute only related to the definition of the function, without pointed out in the prototype of the function, the following description produces a compiler error:
__DECLSPEC (Naked) int function (); / * Error -Naked property is not allowed in function description * /
Microsoft End
Use the rules and restrictions of Naked functions
Microsoft Special Office
* The RETURN statement is not allowed in a naked function. However, the return value can be moved to the EAX register before the RET instruction.
* The exception handling instructions of the structure are not allowed in a naked function, as the instruction must perform cancel internal operations in the stack frame.
* Not allowed in a naked function to use SetJMP run function because it must perform cancel internal operations in the stack frame. But the longjmp run function is allowed.
* The _alloc function is not allowed in a naked function.
* To ensure that the initialization code of the local variable is not present before the preamble, the local variables are not allowed in the function range.
* Do not recommend the frame pointer optimization (/ OY compiler option), but it is automatically canceled for the Naked function. Microsoft End End Writing Preface / End Section Code Consideration
Microsoft Special Office
Before you write your own preface and end sequence sequences, it is important to understand the layout of the stack frame, which is important for understanding how to use __local_size predefined constants.
Stack frame layout
This example shows the standard preface code that can occur in a 32-bit function:
Push EBP; Storage EBP
MOV EBP, ESP; Setting Stack Frame Pointer
Sub ESP, Localbytes; Assign space for local variables
Push; Storage Registersloc
The Lbytes variable indicates the number of bytes required on the stack, and the registers variable is the location occupant that represents the position of the register table on the stack. After the register is subtracted, you can put any other appropriate data in the stack. The following is the corresponding end of the code:
POP; Restore Registers
MOV ESP EBP; recovery stack pointer
POP EBP; Restore EBP
Ret; return from the function
The stack is always pushed down (from high to low memory address). The base pointer (EBP) points to the value of the EBP, and the local variable region starts at EBP-2. In order to access the local variable, the offset from the EBP is calculated by EBP.
__Local_size constant
The compiler provides a constant __local_size for the combined assembler block. This constant is used in the custom preamble code to allocate space on the stack in the stack.
The compiler determines the value of __local_size, which is the total byte number of the total bytes of the temporary variable generated by all user-defined local variables and compilers. __Local__size is only used as an immediate operand; it cannot be used in an expression.
You cannot change or redefine the value of this constant. E.g:
MOV EAX, __local_size; immediate operand - correct
MOV EAX, [EBP -__ local_size]; error
Examples of the next Naked function include custom preamboats and end sequences: __ decspec (naked) func () {INT i; int J; __ASM / * preamble * / {Push EBP MOV EBP, ESP SUB ESP, __ local_size} / * function body * / __ASM / * End section * / {MOV ESP, EBP POP EBP RET}}
Microsoft End
Store
The store in the function definition gives the function to the extern or static storage class.
grammar:
Function definition:
Description Indicator OPT Property Sequence OPT Description Description OPT Composite Statement
/ * Property sequence is Microsoft Special Office * /
Description Indicator:
Storage class indicator Description Of OPT
Type Indicator Description Indicator OPT
Type modifier description indicator OPT
Storage class indicator: / * For function definitions * /
Extern
Static
If a function definition that does not include the storage class indicator, the storage class defaults to extern. You can explicitly explain that a function is Extern, but it can not be required.
If a function explains that the identifier of the stored class indicator extern, the identifier, and any file range has the same connection. This identifier has an external connection if there is no discounted note. If an identifier has a file range and does not store the class indicator, the identifier has an external connection. External connections mean that each instance of the identifier indicates the same object or function. For more information on connection and file scope, see "Survival, Scope, Visibility, and Connection" in Chapter 2 "Structure Structure".
A function of a block range is incorrect with a memory class indicator with a non-Extern. A function with Static storage classes is only visible only in the source file defined. All other functions, whether they are explicitly or implied, which are visible in all source files in the program. If it is expected to be a Static storage class, it must be described in the first appearance of the function description (if any) or the function is defined.
Microsoft Special Office
When Microsoft is allowed, there is no function without a storage class (or extern storage class) if the definition of the function is in the same source file, and the definition explicitly points out the static storage class, the function is given to static Store class.
When compiled with the / ZE compiler option, the function in one block is globally visible in a block.
This is not the case when compiled with / za. This feature is unreliable if the transplantability of the source code is considered.
Microsoft End
Return type
The return value of a function establishes the dimensions and types of the function return value, corresponding to the type indicator in the following syntax:
grammar
Function definition:
Description Indicator OPT Property Sequence OPT Description Description OPT Composite Statement
/ * Property sequence is Microsoft Special Office * /
Description Indicator:
Storage class indicator Description Of OPT
Type Indicator Description Indicator OPT
Type modifier description indicator OPT
Type indicator:
Void
charr
Short
int
Long
Float
Double
Signed
unsigned
Structure or joint indicator
Enumeration indicator
TypedEf name
Type indicators can indicate any basic, structural, or combined type. If you don't include a type indicator, the type of type returned is int.
The return value given in the function definition must match the return value described elsewhere in the program. A function returns a value when an Return statement containing an expression is executed. When the expression is evaluated, if desired, the type of return value is to be converted and returned to the function called by the function. If a function is illustrated as the return type is Void, a return statement containing an expression will generate a warning, and the expression of the expression is not obtained.
The following example illustrates the return value of the function:
TypedEf struct {char name [20]; int id; long class;}
STUDENT;
/ * The type returned is student * /
Student Sortstu (Student A, Student B) {RETURN ((A.ID This example defines the Student type with a TypedEf and defines the function Sortstu has the STUDENT return type. This function selects and returns one of its two structural parameters. In the subsequent calls of the function, the compiler performs detection to ensure that the parameter type is Student. Note: The efficiency can be improved by transmitting the pointer to the structure instead of the entire structure. Char * Smallstr (Char S1 [], Char S2 []) {INT I; I = 0; While (S1 [i]! = '/ 0' && S2 [i]! = '/ 0') i ; IF ( S1 [i] == '/ 0') RETURN (S1); Else Return (S2);} This example defines a function that returns a pointer to a character array. This function has two character array (string) parameters and returns a shorter pointer in two strings, and a array pointer points to the first array element and has its type; therefore, the return type of the function is char Type pointer. For a function returns an int type, you don't need to indicate its int return type before calling them, although the prototype is recommended to detect the correct type detection of the parameters and return values. Parameter The parameter is the value of the value transmitted to the function through a function call, and the parameter is the value expected to be accepted. In a function prototype, the parentheses followed by the function name contain the parameters of the function and its type of full table. The parameter statement indicates the type, size, and identifier of the value stored in the parameter. grammar Function definition: Description Indicator OPT Property Sequence OPT Description Description OPT Composite Statement / * Property sequence is Microsoft Special Office * / Reference: Pointer OPT direct specification Reference: / * A function specifier * / Direct specifier (parameter type table) / * new specifier * / Parameter type table: / * A parameter table * / Parameters Table Parameters Table,... Parameters Table: Parameter Description Parameter table, parameter description Parameter Description: Description Indicator, specifier Description Indicator, abstract specifier OPT Parameter Type Table is a sequence of parameters separated by commas. The format of each parameter in the parameter table is as follows: [Register] Type Indicator [Design] Description of the function parameter generates an error with the Auto property, the identifier of this parameter is used in the function body to give the value of the function. You can't name a parameter in the prototype unless these names exceed the end of this description. Therefore, the parameter name can be assigned in the same manner or different ways in the function definition. These identifiers cannot be redefined in the block of the outermost layer of the function body, but they can be in the inner, nested blocks like the parameter table is a redefined block. There must be an appropriate type indicator before each identifier in the parameter type table, as explained below: Void New (Double X, Double Y, Double Z) { / * Here is a function body * / } If at least one parameter appears in the parameter table, the table can end with three stations (, ...) in a comma. This configuration is called "omitted usage", which points to the number of variable parameters of the function (for more information, see "Use Variable Number Pass Call" later in this chapter). However, the number of parameters that call the function must have at least the number of parameters before comma. If there is no parameter transferred to this function, the parameter table is replaced by the keyword void, and the use of this VOID is different from the use as a type indicator. The order and type of parameters include any omitted usage, which must be the same in all function descriptions (if any) and function definitions. The type of parameters after common arithmetic conversion must be compatible with the type of corresponding parameters (see "Common Arithmetic Conversion" in Chapter 4 "Expression and Assignment" in Chapter 4, and the number of parameters after the omitting number is not detected. . One parameter can have any basic, structural, combined, pointer, or array type. If necessary, the compiler performs independent common arithmetic conversion at each parameter and each parameter. After the conversion, there is no parameters shorter than INT, no parameters have a float type unless the parameter type is explicitly specified as Float in the prototype. For example, this means that a parameter is CHAR and indicates that it has the same role for INT. Function body A "function body" is a composite statement that contains the statement executed by the function. grammar Function definition: Description Indicator OPT Property Sequence OPT Description Description OPT Composite Statement / * Property sequence is Microsoft Special Office * / Composite statement: / * Functional body * / { Description Table OPT statement table OPT} The variable described in a function body, "local variable" has an Auto storage class unless otherwise specified other storage types. When a function is called, stored for local variables and perform local initialization. Perform control is transmitted to the first statement in the composite statement and proceeds until an RETURN statement is encountered or the function is encountered. Then control returns to the function called. If the function has a return value, a return statement containing an expression must be performed. If there is no return statement execution or return statement does not include an expression, the return value of the function is uncertain. Function prototype A function description is before the function definition indicates a function name, return type, storage type, and other properties. For use as a prototype, the function description must also establish the type and identifier of the function parameter. grammar Description: Description Indicator Property Sequence OPT Initial Description Table OPT / * Property sequence OPT is Microsoft Special Office * / Description Indicator: Storage class indicator Description Of OPT Type Indicator Description Indicator OPT Type modifier description indicator OPT Initial Description: Initial explanatory Initial explanatory table, initial set Reference: Specify Reference = Initializer Reference: Pointer OPT direct specifier Direct explanatory: / * A function specifier * / Direct specifier (parameter type table) / * New Description * / Direct Relatable (Identifier OPT) / * Abolished Indicator * / The prototype and function definition has the same format, except that a semicolon is directly fed to the parentheses, so the prototype does not have a statement. In each case, the return value must be consistent with the return value specified in the function definition. The function prototype has the following important use: * They establish a function to return a non-int type return type, although the function returns an int value does not require a prototype, but it is recommended to have prototypes. * No prototype, standard conversion, but do not try to detect the type and number of parameters for parameters. * The prototype is used to initialize the function of the function before the function definition. * Parameter table is used to detect the parameters in the definition of function definitions to detect the corresponding parameters in the function call. The type of each parameter conversion determines the interpretation of the function call parameters placed in the stack. A type mismatch between the parameters and parameters is caused by the incorrect explanation in the stack. For example, in a 16-bit computer, if a 16-bit pointer is transmitted as a parameter, then the opening 32 bits in the stack are interpreted as a long parameter as a long parameter. This error is not only problematic to the long parameter, but also has problems with the subsequent parameters. You can use all functions to explain the full function prototype to detect this error. A prototype creates a function of a function to detect the detection parameter type and the return type without matching the function called before its definition (or in other source files). For example, if you point out the Static storage table indicator in a prototype, you must specify the Static storage class in the function definition. The full parameter description (INT A) can be used in combination with the abstract setup (int) in the same description. For example, as explained below is legal: INT Add (int A, int); This prototype can include two forms, one is type, one is an identifier, each expression as a parameter transmission. However, the range of such an identifier is only to the end of the description. The prototype can also reflect the number of participants is a variable fact, or there is no transfer. There is no such table that does not reflect the mismatch, so the compiler cannot generate diagnostic messages about their diagnostics. For more information on type detection, see "Parameters" behind this chapter. Now the prototype range of the Microsoft C compiler is compiled with ANSI when using the / zA compiler option. This means that if you explain an Struct or UNION flag in a prototype, the flag enters this range instead of a global range. For example, when compiling with / za consistency, you don't call this function that does not get a type of non-match error: Void Func1 (Struct S *); To correct your code, define or instructions for Struct or Union: Structs before this function prototype; Void Func1 (Struct S *); Under / ZE, the flag still enters the global range. Function call A function call is an expression that transmits control and parameters (if any) gives a function, it has formatted: Expression (Expression Table OPT) The expression here is a function name or a function address, and the expression table is a table of expressions (separated by comma). The final value of these expressions is a parameter transmitted to the function. If the function does not return a value, then you explain that it is a function that returns Void. If an explanation exists before the function call, but does not give information about parameters, any uninumened parameter simply undergone common arithmetic conversion. Note: The expression in the function parameter table can be evaluated in any order. Therefore, its value changed by the side effect of other parameters has an uncertain value. The order of order definitions defined by the function call operator only guarantees that all side effects in the parameter are evaluated before controlling to the called function (note, the order of the parameters push into the stack is another thing). See Chapter 4, "Square" in Chapter 4, "Expression and Assignment". The only requirement in any function call is that the expression before parentheses must evaluate the value of a function address. This means that a function can be called via any function pointer expression. example This example describes the function call, which calls the function from one Switch statement: Void main () {/ * Function prototype * / long lift (int), Step (int), DROP (INT); Void Work (INT i)); int SELECT, COUNT ;. SELECT = 1; Switch (select) {Case 1: Work (count, lift); break; case 2: work (count, step); break; case 3: work (count, drop); / * Enter the next CASE * / default: Break;}} / * Function Definition * / Void Work (INT NUMBER, Long (* Function) (INT i)) {Int i; long j; for (i = j = 0; i In this example, the function calls in the main: Work (count, lift), It transmits an integer variable count and a function LIFT address to function work. Note that the address of this function is simply transmitted by the given function identifier, since a function identifier evaluation is a pointer expression. In order to use a function identifier in this way, the function must be explained or defined before the identifier is used; otherwise, the identifier is not recognized. In this case, a prototype of Work is given at the beginning of the main function. The parameter function in the WORK shows a pointer to a function, which has an int parameter and returns a long value. The parentheses that enclose the name of the parameter is needed; without them, this note indicates a function that returns a long value pointer. Function Work From the FOR cycle to call the selected function: (* function) (i); A parameter i is transmitted to the called function. Parameter The parameters in a function call have this format: Expression (Expression Table OPT) / * Function Call * / In a function call, the expression table is a table of expressions (separated by commas). The final value of these expressions is sent to the function of the function. If the function is not a parameter, the expression contains the keyword void. A parameter can be any value having basic, structural, combined, or pointer type. All parameters are transmitted via values. This means that the copy of the parameter is assigned to the corresponding parameters. This function does not know the actual memory location of the transferred parameters. This function uses this copy without affecting the original export variable. Although you cannot transmit array or functions as a parameter, you can transfer their pointers. The pointer provides a function to access a value by reference. Due to the pointer of a variable saves the address of the variable, the function uses this address to access the value of the variable. The pointer parameters allow a function to access arrays and functions, although arrays and functions cannot be transmitted as a parameter. The order of parameter evaluation can change in different compilers and different optimization layers. However, the parameters and any side effects must be completely evaluated before entering the function. See "Side Effect" in Chapter 4, "Expression and Assignment". The expression in a function call is evaluated and performs common arithmetic conversion on each parameter in the function call. If an prototype is available, the result parameter type is compared to the parameters corresponding to the prototype. If they do not match, perform a transition or a diagnostic message. The parameters also experience common arithmetic conversion. The number of expressions in the expression table must match the number of parameters. Unless the prototype or definition of the function is explicitly pointed out that the number of parameters is variable, in this case, the compiler detects a variety of parameter tables, if necessary, convert it. For more information, see "Variable Number Parameter Call" in the next section. If the parameter table of the prototype contains only the keyword void, the compiler looks forward to 0 parameters in the function call, and 0 parameters are expected in the definition. If you have discovered any parameters, the compiler issues a diagnostic message. example This example uses a pointer as a parameter: Void main () {/ * function prototype * / void swap (int * number, int * Num2); int x, y;.. Swap (& x, & y); / * function call * /} / * Function Definition * / Void swap (int * num1, int * Num2) {INT T; T = * Num1; * Num1 = * Num2; * Num2 = T; In this example, the SWAP function illustrates in main, there are two parameters, indicating NUM1 and NUM2, respectively, both of which are INT values. In the prototype definition, the parameters Num1 and Num2 also illustrate the pointer to the int type value. In the function call: SWAP (& X, & Y); X 's address store in Num1, Y's address is stored in Num2, and now there are two names or "alias" in the same location. The reference to * Num1 and * Num2 in SWAP effectively reference the X and Y in Main. The assignment in SWAP actually swaps the contents of X and Y. Therefore, it is not necessary to use the RETURN statement. The compiler performs type detection on SWAP parameters, and the prototype of SWAP includes the parameter type of each parameter. The identifier in the parentheses in this prototype and definition may be the same or different. It is important to match the type of parameters in prototypes and definitions must match the types of parameters. Call with variable number parameters Some parameter tables can be terminated by omitting the marking. A comma follows three stations (, ...) pointing out that there can be more parameters to transmit to functions, but they do not give more information. Type detection is not performed on each parameter. There must be at least one parameter in front of the omitted identity, the omitted tag must be the last language symbol in the parameter table. There is no omitted tag, and if it accepts parameters except the parameters described in the parameter table, the function is uncertain. In order to call a function with a variable number of parameters, simply indicate any number of parameters in the function call. An example is a PrintF function of the C run library, which calls for each type name in the parameter table or parameter type must contain a parameter. All parameters specified in the function call are placed in the stack unless the __fastcall call convention is specified. The number of function descriptions determines how many parameters taken from the stack and assign them. You have to be responsible for accepting any additional parameters from the stack and determining how many parameters. The stdargs.h file contains an ANSI mode macro that accesses a variable function with variable parameters. Similarly, it is also supported the Xenix Macro included in varargs.h. This sample illustrates a function that calls variable parameters: Int averagc (int first, ...) Microsoft Special Office In order to maintain compatibility with Microsoft C, an ANSI C standard a Microsoft Expansion is the end of the allowable parameter table with a comma (,) indicating a variable parameter. However, it is recommended to change this code to the omitted tag. Microsoft End recursive function Any function in the C program can recursively call, that is, it can call itself. The number of recursive calls is limited to the size of the stack. For information on setting the linker options for stack size, see the "Stack Assignment" (/ stack) linker option in the online "Microsoft Visual C 6.0 Programmer Guide". Each time you call this function, allocate new storage for its parameters and auto and register variables so that the unfinished calls in front are not covered. The parameters can only directly access instances of establishing their functions. The previous parameters cannot directly access the following function instances. Note that there is no new storage when there is a static store description. Their storage is in the survival of the program, and each reference for such variables accesses the same storage area. example This example shows recursion calls: INT factorial (int Num); / * Function prototype * / void main () {int result, number; result = factorial (number);} INT FACTORIAL (INT NUM) / * Function Definition * / {IF ((NUM> 0) || (Num <= 10)) Return (Num * Factorial (NUM-1));}