-1

Where exactly should the prototypes be declared? For example right after the include statments, or right before the main method? I know they both compile but is one considered more standard or more redable?

#include <iostream>
#include <cstdlib>

const unsigned int PI = 3.14;

using namespace std;

int myFunc(int x, int y);
long factorial(int n);

int main()
{
  //...
  return 0;
}

or

#include <iostream>
#include <cstdlib>

int myFunc(int x, int y);
long factorial(int n);

using namespace std;



int main()
{
  //...
  return 0;
}

or shoudl they not be used at all and main should be declared last?

No one has really addressed if one way is more readable or prefered.

Celeritas
  • 14,489
  • 36
  • 113
  • 194

3 Answers3

1

It would only matter if you actually used a type from the std in your function prototypes. In your example you don't, so it doesn't matter in your case.

This would not compile:

#include <string>

void foo(string const & s);

using namespace std;

But this would:

#include <string>

using namespace std;

void foo(string const & s);

But you shouldn't use using namespace std anyway.

Community
  • 1
  • 1
cdhowie
  • 158,093
  • 24
  • 286
  • 300
0

A function prototype must be declared before the function usage. It may be declared even in a block scope.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

In the example you have shown it doesn't matter.

The rule is:

A function has to be declared before you use it (in this case: call it).

Note: If the function is defined before its usage then you don't need explicit function declaration. The function definition serves the purpose.

Alok Save
  • 202,538
  • 53
  • 430
  • 533