tags:

views:

138

answers:

4

I'm a Java programmer, learning opengl in C for the first time. I wanna dissect this simple code that my instructor gave me without much explanation:

void renderScene (void) {
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
        glVertex3f(-0.5,-0.5,0.0);
        glVertex3f(0.5,0.0,0.0);
        glVertex3f(0.0,0.5,0.0);
    glEnd();
    glFlush();


   void init(); {
       int submenu;
       submenu = glutCreateMenu(menuApp);
       glutAddMenuEntry("Option A",1);
       glutAddMenuEntry("Option B",2);
       glutAddMenuEntry("Option C",3);

       glutCreateMenu(menuApp);
       glutAddSubMenu("SubMenu",submenu);
       glutAddMenuEntry("Salir",4);

       glutAttachMenu(GLUT_RIGHT_BUTTON);


   }

}

Questions:

a) What does void renderScene (void) means? Why should this function take a void paramether?

b) What the hell is void init(); {}? Why both ; and {}? Why is it inside the renderScene function?

+2  A: 

void renderScene (void) is C's way of saying "declare renderScene as a function taking no arguments and returning no value". void renderScene() would be something different - it is an obsolete function declaration style in C, declaring that renderScene takes a fixed-but-unspecified number of parameters.

The other part is a little odd, and formatted in a misleading way. Formatted correctly, it would read:

void init();

{
     int submenu;
     submenu = glutCreateMenu(menuApp);
     glutAddMenuEntry("Option A",1);
     /* ... */
}

The block inside the { } is unrelated to the void init();. The void init(); declares a function called init (it's the obsolete function declaration style mentioned above). A function declaration is just a way of saying to the compiler "there's a function somewhere called this, and this is what its arguments and return value are". Since the init function is never called, this line could be omitted.

The block inside the { } is just a compound statement. All it does is open a new scope - the variable submenu has a scope restricted to that block.


It's also worth pointing out that the declaration void init(); is itself an anachronism - it's a declaration that appears after code in the same block, which is a feature added in the 1999 revision of the C standard, but it's a declaration without a prototype, which as mentioned predates the original 1989 C standard.

Note also that function declarations that aren't at file scope are themselves somewhat unusual, although completely legal.

caf
does writing a function without void in its parameters *still* has the meaning that it can take an unspecified number of parameters?
omgzor
You misread, I said *without* void.
omgzor
like declaring int v(){} and calling it with v(1,2,3,4)
omgzor
@omgzor: Only in C, and I'd strongly discourage that use. If it takes variable parameters, use the ellipsis. If it takes no parameters, use `void`. That way there's no ambiguity.
Ben Voigt
@omgzor: Yes, correct. However such a declaration is obsolete - it is maintained for backwards compatibility with pre-ANSI-standard C, but new code should not use it. It does not confer any advantages - you must still call the function with the correct types and number of arguments, it's just that the compiler can't check that you got it right.
caf
+1  A: 

The function renderScene having the parameter void means it does not take any parameters.

The init function initalizes the program. In other words it starts up and gets ready to do something.

The init function should be defined

void init(){
...

}

instead of

void init();{
...
}

and it SHOULD NOT be within renderScene.

erai
Thanks. This freaking guide says init() *should be inside of void renderScene(void)"* :/
omgzor
It's valid C; the braces just open a new scope, as @caf describes. I wonder, given the formatting and explicit mention in the guide, if it's an (annoying) trick question?
Michael Petrotta
@Michael Petrotta: An alternative interpretation is that the useless declaration of `init` is an idiosyncratic form of documentation - ie. the equivalent of `/* init */ { ...`
caf
+1  A: 

void renderScene (void) means the function takes no arguments.

It's similar to void renderScene () in Java.

What the hell is void init(); {}?

Probably a typo, The ; shouldn't be there.

Why is it inside the renderScene function?

Probably another typo, the renderScene should have another }

nos
It's perfectly valid, see my answer.
caf
You're right that it's valid. I'd be surprised if it was intended though. You'd normally not want to set up a glut menu every time you call renderScene.
nos
A: 

If the code snippet you posted is correct, it is the definition of the renderScene function. The function does not return a value and does not accept any parameters.

void init(); is a declaration. It just lets the compiler know the details of the function named init so that it can generate the correct code when we call it -- but it's an incomplete declaration (not much help to the compiler) and it's not called anywhere the declaration is in scope. So, it's basically just noise.

If we ignore that and continue with the {, the next lines are a group of statements initiated probably to introduce the submenu variable, which only lives inside this new block.

pmg