Quote:
|
Originally Posted by CmptrGk
the only reason i used the doubles was because the i wouldnt get any results for some larger numbers. i will also try to make seperate functions for each part (once i learn how to use them). and i did not know that you could instalise(is this the correct term) variables within functions.
|
Yeah, you can declare (your words was correct, if slightly misspelled

) a variable inside a function. This is greatly preferred by most authors, as it helps to keep everything modular and reusable. A variable declared inside a function, as foosFavorite is in this example...
PHP Code:
void foo(void);
void bar(void);
int main(int argc, char *argv[])
{
foo();
foosFavorite = 1; // Compile error - foosFavorite doesn't exist
}
void foo(void)
{
// Decalare a variable
int foosFavorite;
foosFavorite = 1; // Fine
// Call bar()
bar();
}
void bar(void)
{
foosFavorite = -100; // Compile error - foosFavorite doesn't exist
}
...is visible only to that function. For instance, in my example here, foosFavorite is able to be used only by foo(), not by the function which called it nor by the function foo() calls.
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.
