15. C # basic sorting (recursion ),
Functions with output parameters
The input parameter is equivalent to a function, and is equivalent to a variable that has been assigned a value. It is directly available.
The output parameter is equivalent to defining a variable without a value, assigning a value to the function, and then bringing the value out of the function when calling the function.
Example:
Public void shuchu (int a, out int B) {B = a + 10; // B must be assigned a value}
Writing in the main function:
Static void Main (string [] args) {int a = 11, B; Program p = new Program (); p. shuchu (a, out B); // B needs to be defined first. The data type of the corresponding function is used to receive the transmitted data Console. writeLine (B); // 21}
Exercise: Use the output parameter to write the method for solving the quadratic equation of a dollar (returns the value of X 1, x 2)
Public string fangcheng (int a, int B, int c, out double x1, out double x2) {double de = (double) B * B-4 * a * c; if (a = 0) {x1 = x2 =-1; return "not a quadratic 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 "solved ";}}Recursive answer
I. concept:
The function itself is called in the body until it meets a certain condition and does not continue to be called.
** To put it simply, let the function first execute the step that meets the conditions, and then start to call the function with the data.
2. conditions should be met:
(1) There is a process of repeated execution (calling itself );
(2) Conditions for jumping out of the repeated execution process (function Exit)
Iii. Example
Factorial calculation n! = N * (n-1) * (n-2) * (n-3 )*...... * 1 (n> 0)
Iv. Notes notice:
1. A condition for loop end must exist in recursion.
2. Each call to a recursive function requires stack storage. If the number of calls is too large, Stack Overflow may occur.
Exercise:
1. There are n peaches. Each day I eat more than 1 million peaches. Seven days later I have one. How many peaches do I have?
public int tao(int day) { if (day == 7) { return 1; } int sum = (tao(day + 1)+1)*2; return sum; }Answer
2. How many sheep does one sell in a village?
public double yang(int cun) { if (cun == 7) { return 2; } double sum =(double)(yang(cun + 1) + 1) * 3; return sum; }Answer
Reference page: http://qingqingquege.cnblogs.com/p/5933752.html