15,041,788 members
See more:
I was learning about function prototypes and I was wondering what was the difference in using normal function. So basically what is the difference in doing this:

int average(int num1, int num2); // a function prototype

int main()
{
cout << "enter two number: ";
int firstnum, secondnum;
cin >> firstnum >> secondnum;
cout << "average is: " << average(firstnum, secondnum);
return 0;
}

int average(int num1, int num2)
{
return (num1 + num2) / 2;
}

and doing this:

int average(int num1, int num2) // A normal function
{
return (num1 + num2) / 2;
}

int main()
{
cout << "enter two number: ";
int firstnum, secondnum;
cin >> firstnum >> secondnum;
cout << "average is: " << average(firstnum, secondnum);
return 0;
}

What I have tried:

I've tried looking up on YouTube and google but they don't specify the difference
Posted
Updated 28-May-21 2:56am
Richard MacCutchan 28-May-21 9:05am

The only time it is important is when they need to be kept in a header file.

## Solution 2

Quote:
Why use function prototypes instead of normal functions in C++?

Reason is rather simple: C/C++ compilers need to know exactly what are the parameters and return types of a function before using it.
So when a function is defined after using it, you need a prototype.
C++
```int average(int num1, int num2); // a function prototype

int main() {
cout << "enter two number: ";
int firstnum, secondnum;
cin >> firstnum >> secondnum;
cout << "average is: " << average(firstnum, secondnum);
return 0;
}

int average(int num1, int num2) {
return (num1 + num2) / 2;
}```

if function is defined before using it, you don't need a prototype.

thank you for helping

## Solution 3

They were inveted to allow my weird code.
C
```#include <stdio.h>

int boo( int x );

int foo( int x )
{
if ( x < 100 )
return boo( x + 1);
else
return 100;
}

int boo( int x )
{
if ( x % 2 )
return foo ( x * 2 );
else
return (x+1);
}

int main()
{
printf("%d\n", foo(5));
return 0;
}```

Try to remove the `boo` function prototype.

thank you
CPallini 28-May-21 9:16am

You are welcome.
Patrice T 31-May-21 14:45pm

+5
CPallini 3-Jun-21 2:13am

Thank you!

## Solution 1

Function prototypes exist so that you can put the actual functions in any order which makes sense to you: you can group them by what they do, by what data they process, or even alphabetical order if you want.

The important thing is that before you can call a function the system has to know it's signature: what it accepts as a parameter, and what it returns - as well as it's name.
That's where function prototypes come in: they tell the system the method and it's signature before you actually have to define the function body.

And there is one place that this is essential: recursion.
If you have two methods which each call the other, the system can't tell what both function signatures are before they are called unless at least one of them has been prototyped.

It's not essential to use prototyping, but it is useful.
v2

thank you
OriginalGriff 28-May-21 9:53am

You're welcome!