View Single Post
  #6   Spotlight this post!  
Unread 14-10-2004, 14:22
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: i would like your opinion on my programs

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 argcchar *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.
__________________


Last edited by Ryan M. : 14-10-2004 at 14:24.