/*x^n should be less than 32767, otherwise the output will be a negative number. This is because the value range of the int type is limited. Normal pow functions usually use float or double types, and the parameters should also be float or double types. */
#include
int power(int x,int n)
{
if (n>1)
{
return x*power(x,n-1);
}
else
{
if (n>0)
return x;
else
return 1;
}
}
void main()
{
int x,n;
printf("input x,n:");
scanf("%d%d",&x,&n);
printf("%d",power(x,n));
getch();
clrscr();
}
Your recursive program is wrong. I will transfer a correct one with explanation for you to take a look at.
Recursion and calling of language functions
1. Basic content:
Function in C language can be called recursively, that is: you can call yourself directly (simple recursion) or indirectly (indirect recursion).
Key Points:
1. C language functions can be called recursively.
2. It can be called directly or indirectly. Currently only direct recursive calls are discussed.
2. Recursive conditions
Using recursive methods to solve problems must meet the following three conditions:
1. The problem to be solved can be transformed into a new problem, and the solution to this new problem is still the same as the original solution, except that the objects being processed increase or decrease regularly.
Note: The method to solve the problem is the same. The parameters of the calling function are different each time (regular increment or decrement). If there is no pattern, recursive calling cannot be applied.
2. This transformation process can be applied to solve the problem.
Note: Using other methods is troublesome or difficult to solve, but using recursive methods can solve the problem well.
3. There must be a clear condition for ending the recursion.
Note: Must be able to end the recursive call at the appropriate place. Otherwise, the system may crash.
3. Recursive Example
Example: Using recursive method n!
When n>1, n! The problem can be transformed into n*(n-1)! new questions.
For example n=5:
Part one: 5*4*3*2*1 n*(n-1)!
Part 2: 4*3*2*1 (n-1)*(n-2)!
Part 3: 3*2*1 (n-2)(n-3)!
Part 4: 2*1 (n-3)(n-4)!
Part 5: 1 (n-5)! 5-5=0, get the value 1, end the recursion.
Source program:
fac(int n)
{int t;
if(n==1)||(n==0) return 1;
else
{ t=n*fac(n-1);
return t;
}
}
main( )
{int m,y;
printf(“Enter m:”);
scanf(“%d”,&m);
if(m
else
{y=fac(m);
printf(“\n%d! =%d \n”,m,y);
}
}
4. Recursion description
1. When a function calls itself, the system will automatically temporarily retain the current variables and formal parameters in the function. During a new round of calls, the system will use the variables and formal parameters for the newly called function. Open up additional storage units (memory space). The variables used in each function call are in different memory spaces.
2. The more levels of recursive calls there are, the more storage units occupied by variables with the same name. It is important to remember that every time a function is called, the system will open up new memory space for the variables of the function.
3. When the function called this time ends, the system will release the memory space occupied by this call. The program flow returns to the call point of the previous layer, and at the same time obtains the data of the memory space occupied by the variables and formal parameters in the function when entering this layer.
4. All recursive problems can be solved with non-recursive methods. However, for some more complex recursive problems, using non-recursive methods often makes the program very complicated and difficult to read. Recursive calling of functions can solve this problem. It can make the program concise and clear and have better readability when dealing with such problems; however, during the recursive calling process, the system needs to open up memory space for the variables in each layer of calls, remember the return point after each layer of calls, and Adding a lot of extra overhead, recursive calls to functions usually reduce the running efficiency of the program.
5. Program flow
fac(int n) /*Use different parameters for each call*/
{ int t; /*Each call will open up a different memory space for variable t*/
if(n==1)||(n==0) /*Return 1 when these conditions are met */
return 1;
else
{ t=n*fac(n-1); /*Every time the program runs here, this function will be called again with n-1 as a parameter. This is the call point*/
return t; /*Run here only when all processes called in the previous sentence are completed. */
}
}
The above is the detailed content of Implement the power function recursively in C language and call it in the main function. For more information, please follow other related articles on the PHP Chinese website!