Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<iostream>

using namespace std;

int fact(int);

int main()
{
    int a=5,b;
    b=fact(a);
    cout<<b;
    return 0;
}

int fact(int);
{
     int a=6,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;
}


What I have tried:

it showing expected unqualified-id before ‘{’ token
{
Posted
Updated 24-Sep-18 1:50am
v2
Comments
ZurdoDev 24-Sep-18 7:43am    
Your title broke.

Quote:
int fact(int);
{
int a=6,b;
b=fact(a);
cout<<b;
return 0;
}
That is completely wrong on so many layers...

If you remove that, your code, though no beautiful, will work.
 
Share this answer
 
You are declaring a "forward reference" function called fact :
C++
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:
C++
#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;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900