Quick function question

I see this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;

void function();

int main()
{
  //some code
  function();

return 0;
}

void function(){
  //some code
  cout << "something";
}


more than I see this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;

void function(){
  //some code
  cout << "something";
}

int main()
{
  //some code
  function();

return 0;
}


Why is that? Surely the latter is clearer to read and results in tighter code?
Not if you have a bunch of functions. You'd have to scroll through tons of code to get to main(), rather than skipping past a few prototypes.
Last edited on
That is highly a personal opinion.

I think the reason why some people put the function declarations at the top is that you don't have to care about what order you define the functions. If you have two functions calling each other there is no other way than declaring at least one of the functions before the functions are defined.

It will make more sense when you start to use multiple files. Function declarations are often put into a header file so that other files can include that header files to be able to call the functions.
Last edited on
Okay! Thanks both of you for your explanations!
Topic archived. No new replies allowed.