Take notes on the seventh day of IOS Learning (function and Recursion), and on the seventh day of ios

Source: Internet
Author: User

Take notes on the seventh day of IOS Learning (function and Recursion), and on the seventh day of ios

I. Functions

1) concept: encapsulation of code blocks with specific functions

2) Function Definition: Function Type + function name (form parameter list)

Function Name of the function type (parameter type 1 parameter name 1, parameter type 2 parameter name 2 .....){

Function body; // function implementation

}

3) The function name must comply with the identifier definition.

4) The return type of the function can be null, void is used for null, or other data types.

5) Call the void function: function name (); The void type cannot be received by any type.

6) call an int-type function: defines int-type data to receive the return value of the function. For example, int sum = add (x, y). Sometimes, the return value can be ignored.

7) return of the return function. The return Statement of the void function can be omitted or empty. Multiple return statements can be returned, and the first sentence to be executed prevails.

8) for other types of non-void, the return expression type must match the defined type.

9) If the return type does not match the receiving type, an error may not be returned, but the error may cause loss of precision. For example, int-type functions are received by char.

10) benefits of functions: different functions are encapsulated for easy reading and maintenance, and easy division of labor. The caller can ignore the specific implementation.

11) Notes for using functions:

1. The function name must be the same as the function name.

2. The interface should be simplified (parameters and return values)

3. The functions should be single and should not be mixed.

4. The external function interfaces should be annotated in detail.

12) local variables: Starting from the position of the variable definition to the end of "}" of the method body where the definition is located

13) function running process:

1. open up space for parameters

2. Pass the actual parameter value to the formal Parameter

3. Formal parameters involved in Calculation

4. Destroy the parameters during return

14) global variables: the variables defined in the external method can be accessed by all functions, but cannot be modified by all functions.

15). c file write method implementation;. h file put function declaration.

16) Declaration of the function: name of the type function returned by the function (parameter Type 1, parameter type 2, parameter name 2 ....).

17) the function is defined after main, but must be declared before main.

18) content stored in the header file:

1. function declaration

2. Type Definition # typedef

3. macro definition # define

19) functions can be called on each other at a level or on their own.

 

Ii. Recursion

1) concept: a process in which a function calls itself

2) the advantage of recursion is to make the code structure more clear and make the program more concise. The disadvantage is that it occupies too many resources and may cause stack overflow.

3) Use recursive algorithms to calculate the Fibonacci sequence

Implementation Code:

 1 int fblq(int n){ 2    if(n==1||n==2) 3       return 1; 4    else  5      return fblq(n-1)+fblq(n-2); 6 } 7 int main(){ 8   int len=20; 9   for (int i=1; i<=len; i++) {10     printf("%d ",fblq(i));11   }12   return 0;13 }

4) recursively calculate the maximum common divisor of two numbers

Implementation Code:

 1 int ComDiv(int x,int y){ 2     if(x%y==0) 3        return y; 4      else   5        return ComDiv(y,x%y); 6 } 7  8 int main(){ 9    printf(“%d”, ComDiv(28,35));10    return 0;11 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.