Quote:
|
Originally Posted by Ryan Morehart
In C, you have to declare all local variables at the beginning of the function. No other executed line can come before them; comments are fine, but no function calls or anything. C++ relaxes this rule, so if you're actually using a C++ compiler, it will let you get away with not having declarations at the beginning. Be warned though; the MPLAB compiler only accepts at the beginning. 
|
In general though, it's a good habit to declare anything and everything you'll possibly need for the function in the first lines of the function, rather than spread out amongst the random executed lines. It makes the code so much easier to read ;-). Plus, as Ryan said, it's
the way to do it in C, and since the robots are all programmed in C (and I don't ever see that changing, because C has been around for quite some time...).
Also, I'm not 100% certain, but I believe it's faster if all your code is C, instead of C++ (this comes from extremely limited programming for Palm IIIs), but someone would have to confirm this, because I can't really remember the reasons or anything.