function considerations in the design: The function should be designed in a single exit or single entry, as it allows us to read more easily and classify the functions.
1, function definition: function A piece of code, accept one or more parameters, do a thing, and return 0 or more values.
Can be analogous: Become a function in mathematics, we enter multiple parameters and will output the corresponding value, which process is like our math function inside the process of processing.
2. Function definition:
Returns the type function name (parameter, ...). )//Function head
{
function body
}
Eg:int sum (int first, int second)
{
return first + second;
}
3. Calling functions
Function name (parameter)
() play the role of the call function, in fact, there are parameters also need to write () to play the role of the call function
If there are parameters, our parameters need to be sorted in order, because they are corresponding.
Eg: the function sum (10,20) of the call sector;
Note: When calling a function, we should note that the value passed in is in accordance with the type in the form parameter, otherwise there will be a default transformation, between Eg:int and float, double.
This is the funnel for C-language functions, which will not happen in Java and C + +.
4. Return value: Return keyword, if there is a return value, then it is necessary to use the return Guan keyword to be returned.
For example: We often return 0 in the main function, and the following function returns the value.
When there is no return value type, we will not have to return the corresponding value:
5. The function relations in C language:
function compilation in C is compiled from top to bottom , that is, the function we want to call must precede the current function, or declare it in the previous procedure.
eg
6, function prototype, is often used to "declare the function" when the function written, its format note,
1) Composition: function head, and with a good ending constitute the function head.
2) Function: Tell the compiler what this function is like.
3) Composition: Name, parameter (quantity and type), return type, which can be omitted inside the form parameter.
7, pass the value,
1) Each function has its own variable space, and the parameters are in this separate space, which is not related to the other functions.
2) We often call: the formal parameter list in the function parameter table, we often call it: "formal parameter"; The value given when the function is called, becomes the actual parameter.
The relationship between the formal parameter and the actual parameter is: The relationship between the parameter and the value.
8 Local Variables:
1, function each run will produce a separate variable space, the variable in this space, is the function of this operation is unique, called as: "Local Variables"
2, the variables inside the function are local variables, the variables of the formal parameters are also local variables.
9. Lifetime and scope of variables
1) Lifetime: When the variable appears, when it dies.
2) This variable works within this range and can be accessed.
The above two points, for local variables, are in parentheses {},--block.
10.
11. When there are no parameters:
void f (void)//direct indicates no parameters
void f ()//traditional C means that the parameters of the F function are not known and do not indicate no parameters.
????
12, all good arithmetic symbols
1. What is the difference between a comma and a good operator when calling a function?
2. The comma inside the calling function is a punctuation mark, not an operator.
F (A, B) f ((b))
13. About Main
int main () is also a function
Do you want to write int main (void)
The return value has a specific meaning in Unix or Linux.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Functions in the C language