0
1

[–] DrBunsen 0 points 1 point (+1|-0) ago  (edited ago)

Anything that is underneat the main, doesn't get loaded in and has to be loaded into the memory to be able to be used. That is what

int add(int, int);

is doing, saying we got an int returning function called add, and it uses 2 parameters that are ints.

0
0

[–] shmuklidooha [S] ago 

Yeah, but why not just define the whole add(){} function at the start?

0
1

[–] DrBunsen 0 points 1 point (+1|-0) ago 

I don't know, I declare all my functions before to main to avoid doing this, but whatever floats your boat I guess.

0
1

[–] ghost_marauder 0 points 1 point (+1|-0) ago 

This prevents issues for the compiler during cyclic definitions

int f(int a){return g(a);}

int g(int a){return f(a);}

will spit out an error saying stuff was not declared.

int f(int a);

int g(int a);

int f(int a){return g(a);}

int g(int a){return f(a);}

will have no compiler issues. This is a leftover from the days when compilers didn't do any backtracking.

0
0

[–] LoungeAbout ago 

It's really an issue with organizing code.

Every function you are going to use must at least be declared before use, but you don't have to define it's innards until later.

In "real life", you typically are separating out your code into sections or libraries. You would declare a set of related functions and put them together in a header (a ".h" file). You would then define those functions in a source file (maybe ".c"). When you need to change code in one section of your application, you'll only need to re-build that one file before linking together all of your source files.

If you defined the functions before your main, you could potentially have a lot of source code in one gigantic file. For simple programs, I almost always do are you describe: define the code before the main.

0
0

[–] shmuklidooha [S] ago 

Thanks!