+ 1

function declaration

why it is necessary to declare function before main function of if i write any function after main function it result in error .

22nd Jul 2016, 3:45 AM
harshvardhan
3 Answers
+ 4
This is because of the way that C code is compiled. When the compiler is doing its thing to your code it needs to know how much memory that function is going to take on the stack, which the headers take care of (i.e. one int and two doubles worth of memory). This is why all functions must be declared before the call is made. That might sound confusing, but think of it like your program is a recipe. The compiler is trying to know what ingredients to get ready, but you are missing your headers. So the compiler doesn't know if you need one cup of flour or four bags. It refuses to put all of the ingredients on the counter unless you specified how much first. Some languages like c# and java are happy to find these things for you but others like c as well as all scripting languages will not or even cannot.
22nd Jul 2016, 3:58 AM
Colton Lillywhite
Colton Lillywhite - avatar
0
thanks coltin lillyewhite
22nd Jul 2016, 4:03 AM
harshvardhan
0
the same reason with the fact that you don't know the future
22nd Jul 2016, 4:25 AM
_Geometry dash_ _Roh_ (AKA NovaRate0315)
_Geometry dash_ _Roh_ (AKA NovaRate0315) - avatar