Recursive defect: When the stack process is executed more often, the computational amount is too large. But each tail recursion can be written as a loop (using a non-native word is iterative)
The FABONACII sequence is implemented in an iterative way:
#include <stdio.h>int fibonacii (int n) { int temp = 0; int a = 1;//Remember to assign the first two numbers the initial value int b = 1; if (n <= 2) return 1; while (n > 2) { temp = a + b; A = b; b = temp;//avoid written a = temp; b = A; At this point a has changed!=1 and then the assignment to B has no meaning n--;//adjust the value stored in AB to move forward } return temp; int main () { printf ("%d\n", Fibonacii (6)); return 0;}
To implement factorial in an iterative way:
#include <stdio.h>int fac (int n) { int ret = 1; while (n) { ret = ret * N; n--;//ret = 1*n,ret = N (n-1) .... Return back to ret;} int main () { printf ("%d\n", FAC (3)); return 0;}
function recursion and iteration