Click here to Skip to main content
15,908,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Comments
Richard MacCutchan 28-May-21 9:05am    
The only time it is important is when they need to be kept in a header file.

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.
 
Share this answer
 
Comments
iwanttoaskquestions 28-May-21 9:06am    
thank you for helping
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.
 
Share this answer
 
Comments
iwanttoaskquestions 28-May-21 9:06am    
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!
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.
 
Share this answer
 
v2
Comments
iwanttoaskquestions 28-May-21 9:06am    
thank you
OriginalGriff 28-May-21 9:53am    
You're welcome!

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