Hi,

I'm just wondering, why do we bother with function prototypes? Wouldn't it be better if we just defined the functions before the main function instead of having a function prototype and a function definition in separate areas?

For example, instead of the following:

#include <iostream>
using namespace std;

void sayHello();

int main() {
   sayHello();
   return 0;
}

void sayHello() {
   cout << "Hello!" << endl;
}

Why can't we just use this instead:

#include <iostream>
using namespace std;

void sayHello() {
   cout << "Hello!" << endl;
}

int main() {
   sayHello();
   return 0;
}

Thanks for the time and help.

How would you suggest handling the following?

void Foo (int i) {
   if (i > 0) Bar (i - 1); 
}

void Bar (int i) {
   if (i > 1) Foo (i - 1);
}

Wouldn't it be better if we just defined the functions before the main function instead of having a function prototype and a function definition in separate areas?

What if you want to break your program up into multiple files? What if you want to write a library that will be used in multiple programs?

Thanks for the replies, really helped me out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.