First, the function
1) Concept: Encapsulation of code blocks with specific functions
2) Definition of function: function type + function name (formal parameter list)
function type function name (parameter type 1 parameter name 1, parameter type 2 parameter Name 2 ...). ) {
function body;//implementation of function
}
3) function name to conform to the definition of identifiers
4) The return type of the function can be null, empty with void, or other data type.
5) Call of void function: function name (); The void type cannot be received by any type.
6) Invocation of the INT type function: Defines the data of type int to receive the return value of the function, for example: int sum=add (x, y); Sometimes it is possible to ignore the return value
7) Return function returns, the return statement of the void function can be omitted or return NULL, the return statement can have multiple, to execute the first sentence to prevail.
8) Other types of non-void, return expression type to match the defined type
9) When the return type does not match the receive type, it does not necessarily cause an error, but can result in a loss of precision, for example: a function of type int is received with a char type
10) The benefits of the function: the different functions of the package, easy to read and maintain, easy to work, call people can not care about the specific implementation.
11) Note points used by the function:
1, the function name to Yanzhiyouwu, the name and function are consistent.
2. Interface to be condensed (formal parameter, return value)
3, to achieve a single function, do not mix.
4, the external function interface to be by the detailed comment
12) Local variables: from the position of the variable definition to the end of the "}" of the method body where the definition is located
13) The process of running the function:
1. Open up space for formal parameters
2, the actual parameters to the form parameter value
3. Form parameter participation operation
4, return when the formal parameter destruction
14) Global variables: variables defined outside the method body, with the advantage that all functions can be accessed, and that all functions can be modified without security.
(). C file Write method implementation;. h file The declaration of the function.
16) Function declaration: Function return type function name (parameter type 1 parameter name 1, parameter type 2 parameter Name 2 ...).
17) The function is defined after main, but must be declared before main.
18) The contents of the header file storage:
1. Declaration of functions
2. Type definition #typedef
3, macro definition #define
19) functions are lateral and can be called by each other, or they can call themselves.
Second, recursion
1) Concept: The function calls itself a process
2) The advantage of recursion is to make the code structure level clearer to make the program more concise, the disadvantage is that it takes up too much resources, can cause stack overflow.
3) using recursive algorithms to calculate Fibonacci sequences
Implementation code:
1 intFBLQ (intN) {2 if(n==1|| n==2)3 return 1;4 Else 5 returnFBLQ (n1) +FBLQ (n2);6 }7 intMain () {8 intlen= -;9 for(intI=1; i<=len; i++) {Tenprintf"%d", FBLQ (i)); One } A return 0; -}
IOS Stage Learning Seventh Day notes (functions, recursion)