Thread: Globals, locals and prototypes

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up Globals, locals and prototypes

    I don't understand globals, locals and prototypes. If you are able to, you please explain them to me and/or point me in the right direction of a good tutorial on the subject.

    Thanks
    -Chris

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    Nothing too much to exaplin here, but let's walk you through it.

    Global variables are those variables which any function in a source file can use, whereas local variables are confined within the function they were declared in. (Side note: in order to use a variable in another source file as well, declare it with an extern keyword infront of it. ex: extern int myint; )

    prototypes are telling the compiler exactly what sorts of parameters a function takes so it knows whether you're using it wrong in your source. an alternative to doing that is to define the function before your main code, so you could have either:
    Code:
    int myfunc(int);
    
    int main(void)
    {
        /*code*/
    }
    
    int myfunc(int counter)
    {
        return counter++;
    }
    or

    Code:
    int myfunc(int counter)
    {
        return counter++;
    }
    
    int main(void)
    {
        /*code...*/
    }
    And that's pretty much all there is to it.

Popular pages Recent additions subscribe to a feed