Can anyone explain to me this code ? I wrote it but I copied some from another source. It won't compile due to a linker error in dev c++, but I'm sure I did it write. I don't understand the concept of functions. for e.g "int factorial (int) <-- why is there (int) there again? And if it was int (x, y) what purpose do they serve? ty..
#include <iostream>
using namespace std;
int factorial (int);
int main()
{
int result;
cout << "Enter a number to calculate the factorial, then press ENTER: ";
cin >> result;
cout << "The factorial of " << result << "is" << factorial (result) << endl;
}
int factorial (int n)
{
int fact = 1;
if (n <= 1)
return 1;
else
fact = n * factorial (n - 1);
return fact;
}
Discard Dev C++, use CodeBlocks instead. I see that your knowledge of functions is not complete. You should read some books. try this link http://www.cplusplus.com/doc/tutorial/functions/
Also, there is "int" in the function because its a declaration of the function. While declaring you can omit the name of the parameters.
1 2 3 4 5 6 7 8 9
//For declaration
int factorial(int);
//and
int factorial(int n);
//both are valid