I was looking through the example C source code in the CE Developer kit, and noticed that while function prototypes were normally used, occasionally an entire function would be declared before the main loop. How do you know when a function prototype needs to be used or not?

Here is the example where a prototype was not used.

Code:

/* Function for drawing the main sprite */
void draw_sprite(int x, int y) {
    /* Fill the screen with color index 0
    /* Note that this is a super inefficient way to
    /* redraw, but it demonstrates a simple way of clearing past data */
    gfx_FillScreen(0);

    /* Draw a bunch of different styled sprites on the screen */
    gfx_Sprite(ubuntu, x, y);

    /* Copy the buffer to the screen */
    gfx_BlitBuffer(); // This is the same as gfx_Blit(gfx_buffer)
}

void main(void) {
    bool right;
    bool left;
    bool up;
    bool down;
    kb_key_t arrows;

    ...


Thank you Very Happy
A function must either be declared (prototype) or fully defined (like in your example) before it can be used. It doesn't matter which. For quick and dirty code, usually you'll just define stuff before main and skip the prototype. For larger or just more "proper" code, you'll usually use prototypes; especially once you start getting into multiple source files, when usually each logical group of code declares all its prototypes and globals in a .h file and defines them in a .c file.
Thanks so much for the reply. Makes sense to me Very Happy
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement