I've learned about recursion for some time.In short ,it's a function or way that operates by calling itself again and again. But I actually know how it operates today.Take factorial for example:
#include <iostream>
using namespace std;
int factorial(int n)
int mian()
{
int x;
cin>>x;
cout<<factorial(x)<<endl;
return 0;
}
int factorial(int n)
{
if (n==0||n==1) return 1;
if(n<0) return 0;
return factorial(n-1)*n;
}
If you input number 3,it will firstly enter factorial(3),while in factorial(3) it has factorial(2) and in factorial(2) it has factorial(1).Until factorial(1) it return 1and 1 will be maintain ed in a temporary variable .Then it will return to factorial(2) and multiply the last temporary variable .The same as factorial(3).
It includes the operation of stack,FILO(first-in ,last-out).