1

While going through some allegro tutorials I found an odd call.

int al_init();

The function al_init() initializes allegro so that the allegro functions can be used. What is with the int al_init(); line? If I change this line of the code to exclude int it works the same, but if I take out the line altogether it does not work. What is this line doing? The only thing I can imagine is that it creates an integer and assigns it the return value of the al_init() function, with that likely being -1 for failure and 0 for success etc. But if that is what this is doing, then how can you even check the return value?

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
Ty_
  • 828
  • 1
  • 11
  • 21
  • 3
    Sure looks like a function prototype/declaration instead of a function call or definition to me. – A. Webb Nov 05 '12 at 03:30
  • How does it fail to work? I bet it fails to compile, right? – Omnifarious Nov 05 '12 at 03:45
  • `al_init()` is a macro that expands to `al_install_system(ALLEGRO_VERSION_INT, atexit)`. If you include the Allegro headers properly there's no need to include that line. – Matthew Nov 06 '12 at 01:34

3 Answers3

2

In C/C++ there are function declarations and function definitions:

Declarations look like these:

int a_function();
void another_function();
double yet_another_function();

Explanation:

  • The identifier before the function name (e.g. int, void, double) describes the type of value returned by the function.
  • If you do not specify one, it defaults to int (which is why it works when you remove int from int al_init(), but not when you remove the declaration altogether)
  • void means it's not supposed to return a value (even though technically it can, but that's for rarer cases)

Definitions look like these:

int a_function() {
    std::cout << "hello world!";
    int x = 1;
    int y = 2;
    return (x + y);
}

Notice the difference:

  • Declarations end with ;
  • Definitions are followed by a block of code enclosed by braces: { and }, but no ;!
  • In some cases, you do not need to declare a function. If it's at a point in the code where the definition has already been seen, the definition can substitute for the declaration (this leads us to the next point...)
  • The purpose of declaration is to tell the program that, for example, there is a function named a_function, it expects no arguments, and it returns a value of type int.
Martin York
  • 257,169
  • 86
  • 333
  • 562
sampson-chen
  • 45,805
  • 12
  • 84
  • 81
1

Are you sure you aren't looking at the function declaration?

Connor Hollis
  • 1,115
  • 1
  • 7
  • 13
1

According to C's syntactic rules, a prototype is defined as:

<return-type> function-name (param1, param2, ..);

And a function call is defined as:

function-name(param1, param2,...);. So its obviously defining a function prototype and NOT calling the function.

C89 and previous rules were:

With the above rule, another rule was: For implicit integer return type, the prototype was suppose to be defined outside of any executable function code. If it was inside of a function, it would be called a function-call and not function-prototype-definition.


That aside, lets start with the question:

While going through some allegro tutorials I found an odd call. int al_init();

That's incorrect, it looks like a function prototype or declaration

The function al_init() initializes allegro so that the allegro functions can be used. What is with the int al_init(); line? If I change this line of the code to exclude int it works the same, but if I take out the line altogether it does not work.

What does not work? Stops to compile? That would be obvious because it will not be able to find al_init() function. Also the reason it works without the "int" is because its implicitly assumed to return an integer.

What is this line doing?

Its telling the compiler that al_init() library function is defined elsewhere, typically in a .lib or .a or .dll, and you would want to use it in your program.

The only thing I can imagine is that it creates an integer and assigns it the return value of the al_init() function,

Absolutely incorrect interpretation of code, it does not create any integer, nor assigns any value to it.

with that likely being -1 for failure and 0 for success etc. But if that is what this is doing, then how can you even check the return value?

Since you want to know what that function returned, you could do this while an actual call to al_init():

int retval = al_init(); printf("al_init() returned %d",retval);

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78