# Function differences help

• 11-17-2012
ZezXion
Function differences help
Alright, so on my way through learning c++, I noticed that functions are done two different ways, but I cannot find if there are differences, or if they are just preferred ways of doing functions.

Here I have two different simple codes done in different ways.
The first way declares the entire function at the top:
Code:

```#include <iostream> using namespace std; int factorial(int num) { int starter; int total = 1; for (starter = 1; starter <= num; starter++) { total *= starter; } return total; } int main() { int num; cout << "Please enter a number greater than 1!" << endl; cin >> num; if (num <= 1) { cout << "Please use a number GREATER than 1." << endl; } else { cout << "Function returned: " << factorial(num) << endl; } system("PAUSE"); return 0; }```
This second way uses what I've read as being a "Prototype" and then has all the information at the bottom of the code.
Code:

```#include <iostream> using namespace std; int factorial(int prototype); int main() { int num; cout << "Please enter a number greater than 1!" << endl; cin >> num; if (num <= 1) { cout << "Please use a number GREATER than 1." << endl; } else { cout << "Function returned: " << factorial(num) << endl; } system("PAUSE"); return 0; } int factorial(int num) { int starter; int total = 1; for (starter = 1; starter <= num; starter++) { total *= starter; } return total; }```
Both of these codes work the exact same way, however, the prototype seems useless, as I could just replace it with the entire function that I want. Not only that, but with the prototype, my (int prototype); seems to never have any use.

So yeah, I'm lost here. Can anyone please explain the differences, if any in this? Do I NEED to use a prototype, or can I just do it the first way that I have it in all cases and still have it work just fine? Thanks in advanced!
• 11-17-2012
Salem
You need either the prototype or the definition before you try and use the function.

Which way you go in a program that fits in a single source file is just a matter of style.

But function prototypes are pretty much essential when you start writing programs which span many source files.
• 11-17-2012
iMalc
Quote:

Originally Posted by ZezXion
Both of these codes work the exact same way, however, the prototype seems useless, as I could just replace it with the entire function that I want..

Absolutely correct. Of course you can't always just put every function before the one that calls it. For example if you have functionA calls functionB, but functionB also calls functionA, then you cant put neither first and must use a prototype. This happens in real world code, notably parsers.

Quote:

Not only that, but with the prototype, my (int prototype); seems to never have any use.
The prototype is there to tell the code that the function exists and what parameters it expects. This is needed due ot the way C compilers worked right from the beginning. Some languages such as D, do not require prototypes as they actually scan the code to find the function itself. C compilers just don't do that.

The variable name isn't technically necessary in the prototype, but it is typically given and is the same as the one in the real function, to make it more self-documenting.