15. C # Basic Collation (Recursive)

Source: Internet
Author: User

Functions with output parameters

An input parameter is equivalent to a function, which is equivalent to a variable that has already been assigned and is directly available
The output parameter is equivalent to defining a variable without a value, assigning it in a function, and then calling the function to take the assignment out of the function

Cases:

 Public void Shuchu (intoutint  b) {   ten//B must be assigned }

The notation inside the main function:

Static void Main (string[] args) {intNew program ();p. Shuchu (A,   out B); // b needs to be defined first, corresponding to the data type of the function, to receive the data passed out Console.WriteLine (b); // +}

Exercise: Write a two-time equation solution with output parameters (returns whether there is a solution, and the value of X1,X2)

 Public stringFangcheng (intAintBintC out DoubleX1, out Doublex2) {            DoubleDe = (Double) b * B-4AC; if(A = =0) {x1= x2 =-1; return "not a unary two-time equation ."; }            Else if(De <0) {x1= x2 =-1; return "de<0, this equation has no solution"; }            Else{x1= (Double) (-B + de)/2*A; X2= (Double) (-B-DE)/2*A; return "have a solution"; }        }
AnswerRecursive

First, the concept:

The function body calls the function itself until a condition is met and no further calls are continued.

* * Simply let the function perform the first step to meet the condition and then start calling the function itself with the data.

Second, should meet the conditions:

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

(2) There are conditions for jumping out of the process of repeated execution (function exit)

Iii. examples

Calculation of factorial n! = N (n-1) * (n-2) * (n-3) *......*1 (n>0)

Iv. Precautions Notice:

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

2, each call of the recursive function needs the stack to store, if the number of times too many words easily cause stack overflow.

Practice:

1, n peach, every day eat 1/2+1, 7 days left one, there are a few peach?

 Public int tao (int day  )        {            if7)            {                return  1;            }             int 1) +1) *2;             return sum;        }
Answer

2, a person to drive a flock of sheep to sell, every village sold 1/3+1 only, 7 villages left 2, there are a few sheep?

 Public Double Yang (int  cun)        {            if7)            {                return  2;            }             double sum = (double113;             return sum;        }
Answer

15. C # Basic Collation (Recursive)

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.