Content: 1. Participate in the numerical delivery between the real parameters; 2. Nested calls of the function; 3. Recursive calls of the function. Description: 1. Numerical invoking instructions in the function is unidirectional; 2. Nested call description Use function can greatly increase the efficiency of the change; 3. Recursive calls are important in the data structure, many problems You can solve the recursive method, but in the C language, it is important
There is no more data structure, and it has not been explained as a key point. Therefore, it is necessary to add other ways to increase the recursive.
solution. Originally intended to use the recursive tune as a focus today, but now I find that the nested call is better.
I. Exchange two numbers: wrong method: #include void swap (int x, int y) {INT T; t = x; x = y; y = t;}
Void Main () {Int A, B; Scanf ("% D% D", A, B); SWAP (A, B); Printf ("% D% D", A, B);}
Correct method: #include int x, y; void swap (void) {INT T; t = x; x = y; y = t;} void main {scanf ("% D% D", & x, & y); SWAP (); Printf ("% D% D / N", X, Y);
Summary: The value transfer between the active participation is one-way passed. Only the value of the sub-function is changed, and it is not shown in the main function. correct
The method is to define global variables to change the function value, so that the global variable changes, the output value has changed.
II. Function nested call Equation problem: #include #include float f (float x) {return ((x-5) * x 16) * x-80;} float XPoint (float x1, float x2) {return (x1 * f (x2) -x2 * f (x1)) / (f (x2) -f (x1));} float root (float x1, float x2) {int i; float x, y, y1; y1 = f (x1); do {x = xpoint (x1, x2); y = f (x); if (Y * Y1> 0) {y1 = y; x1 = x } Else {x2 = x;}} while (Fabs (y)> 0.00001); return x;} void main () {float x, x1, x2, y1, y2; do {printf ("Input x1, x2: "); Scanf ("% f,% f ", & x1, & x2); y1 = f (x1); y2 = f (x2);} while (Y1 * Y2> 0); x = root (x1, x2) Printf ("a root IS% f / n", x);} Reference such a function of such a large segment is to explain the following problem: 1. When the function is defined, the sub-function is independent of each other, not from each other; 2. Both the definition of the function appears before the main function, so you don't have to do type description; 3. Execute the Do-While loop, in turn (Note: Do-while loop is not deep, if you need to re-learn) 4. Use functions Benefits 1: If you solve other equations, you don't have to coding again. As long as you make a slight modification of this procedure, you can solve the ease of equation. Squaining question: Ask cm / n = n! / (M! * (Nm)!) #Include #include float fac (int K) {float t = 1; int I For (i = 2; i <= k; i ) T * = i; returnit ()} void main () {float C; Int m, n; printf ("Input M, N:"); scanf (" % D% D ", & M, & N); C = FAC (N) / (FAC (M) * FAC (NM)); Printf ("% D! / (% D! * (% D-% D)! ) =%. 0f / n ", n, m, n, m, c);} Using the benefits of programming 2: Although the topic involves three steps, only one function can be reused, solve
The problem has greatly added the efficiency of the program.
3. The recursive call of the function refers to the function itself in which the function itself is directly or indirect when calling a function. Whether it is direct recursive or indirect recursive
Often ending, therefore should contain a certain condition to control the end of recursive call.
Age question: #include int Age (int N) {INT C; if (n == 1) c = 10; ESLE C = Age (N-1) 2; Return C;} void main ) {Printf ("% D", age (5));} Although the age problem is more complicated, it is still very simple in the program. This issue has used the thoughts of the stack.
When a fifth person is born, first from the time you go, then then the previous output, the result is obtained.
Squaining problem: #include float fac (int N) {float f; if (n> 1) f = FAC (N-1) * n; // function recursive call ELSE IF (n == 0 || n == 1) f = 1; // Recursively call the end statement. ELSE f = -1; returnif;} void main () {int N; float y; printf ("INPUT A INTEGER NUMBER:"); Scanf ("% D", & n); y = FAC (N); if (Y <0) Printf ("Error:% D <0", N); Else Printf ("% D! =%. 0f", n, y);} The above two examples use the stack, understand the stack The role of recursive calls is conducive to the recursive call of better understanding of the function.