You are declaring a "forward reference" function called
fact
:
int fact (int);
You can't give that a body, or it wouldn't be a forward reference!
Remove the body (and the foreward reference, since you have it twice), and your code will then compile:
#include<iostream>
using namespace std;
int fact (int);
int main ()
{
int a = 5, b;
b = fact (a);
cout << b;
return 0;
}
int fact (int n)
{
int i = 1, j = 1;
while (i <= n)
{
j = j * i;
i = i + 1;
}
return j;
}
But, I'd replace the
while
loop with a
for
:
int fact (int n)
{
int j = 1;
for (int i = 1; i <= n; i++)
{
j = j * i;
}
return j;
}