15-07-12 function, recursive

Source: Internet
Author: User

First, Function:
A function is a relatively separate block of code. Be good at using functions to reduce the amount of repetitive programming segments.

Four elements of a function: function name, parameter (input), return type (output), function body (machining operation)

Functions are typically created in class. It can be simply understood as the output in the main function, input the calculation process in the function, can be called repeatedly to alleviate the workload of writing code.

(a) Definition: [static] Returns the type function name ([variable type variable name [, variable type variable name [...] Variable type variable name]]) {function Body}

(b) Call: [data type variable name =] Function name ([argument 1 [, Argument 2 ...])

Description: 1. The function name is the same as the variable name rule.

2. Real participation in formal parameters must be corresponding.

3.return The return value must be consistent with the return type.

4. Function definitions do not have a certain principle, as long as it works.

5. The parameter is the address, and the variable is preceded by ref;

(c). The function is preceded by a comment:

In order to use this function at a later time do not forget the function of the functions, need to add some comments, so that when using the mouse can be placed on the above to see the written comments to make certain reminders.

Comments are used by entering///, that is, three slashes, and then entering a comment at the top of the function.

(iv). Function One is the value of the pass, and the other is the address:

(1) Pass value is to pass the value of the argument into the function that is called, the value of the argument does not change, the default value is int type, float type, bool type, char character type, structure shape and so on;

(2) The address is transmitted to the called function inside the operation, the value of the argument will follow the change, there are arrays and strings of string;

(3) The above mentioned is involved in the classification of data, divided into value types and reference types, value type is the kind of value, reference types include string types, arrays and objects, the above-mentioned string type is special, mainly

Because the string is actually a character char[] array, it is not directly modified at the time of modification, but the heart opens up a storage space is to create a new string, so it is called, the assignment

When the value is passed, but it is a reference type.

Numeric size sorting:

        Static int[] Paixu (params int [] n)        {for            (int i = 0; i < n.length-1; i++)//bubble sort            {for                (int j = 0; j < n.length-1-I; J + +)                {                    if (N[j] <= n[j + 1])                    {                        int zj = n[j];                        N[J] = n[j + 1];                        N[j + 1] = ZJ;                    }                }            }            return n;        }        static int Bijiao (int a, int b)//formal parameter        {            if (a >= b)            {                return A;            }            else            {                return b;            }        }

Second, function recursion:

First, the concept

The function body calls the function itself until a certain condition is met and the call cannot be resumed.

Ii. conditions to be met

(1). There is a process of repeated execution (call itself).

(2). There are conditions (function exits) that jump out of the iterative process.

Third, the matters needing attention

1. There must be a loop-over condition in the recursion.

2. Each invocation of a recursive function requires a stack to store, and if too many times it is prone to stack overflow.

     Recursively, the function calls itself     //Monkey Eat peach, the park has a bunch of peaches, the monkey eats half a day, and throw away a bad, to the seventh day, the monkey found only a peach, asked the first day how many peaches in the park?

static void Main (string[] args)
{

int n = taozi (1);

 Console.WriteLine (n); } static int Taozi (int day) { if (day = = 7) { return 1; } int n = (Taozi (day + 1) + 1) * 2; return n; }

15-07-12 function, recursive

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.