Consider the program given below, written using the top-down approach, that calculates the factorial of a given integer number using function fact defined in Example. Function fact is called in the main function from a printf function call and the int value returned by it is printed using the %d conversion specification.
Observe that the main function is defined first followed by the function fact, which is called before it is defined. Thus, we need to declare fact in the main function or before it. In the program given above, fact is declared before the main function. The program output is given below.
#include<stdio.h>
int fact(int);
void main()
{
int n,t;
clrscr();
printf("Enter any Number : ");
scanf("%d",& n);
t=fact(n);
printf("Factorial of %d is : %d",n,t);
getch();
}
int fact(int a)
{
int p;
if(a==1)
return(1);
else
{
p=a*fact(a-1);
return(p);
}
}