Globals, locals and prototypes

This is a discussion on Globals, locals and prototypes within the C++ Programming forums, part of the General Programming Boards category; I don't understand globals, locals and prototypes. If you are able to, you please explain them to me and/or point ...

  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,230
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21