A function calls itself is known as recursion.
# include<iostream>
using namespace std;
int fac(int);
int main()
{
int n;
cout<<"enter a number";
cin>>n;
cout<<fac(n);
}
int fac(int n)
{
if(n==1)
return (1);
else
return (n*fac(n-1));
}
The working of this program is as follows. If the value of n is 5 then it calls the function as fac(5). The function returns value 5*fac(4). That means this function calls the function again and returns 5*4*fac(3). This process continues until the value n=1. So the result is 5*4*3*2*1 = 120.