Confusion about function call syntax

I am using this tutorial for SDL: http://lazyfoo.net/tutorials/SDL/04_key_presses/index.php. I am reading through the provided source code for it and there is a part I don't understand.

1
2
3
4
5
6
7
int main( int argc, char* args[] )
{
	//Start up SDL and create window
	if( !init() )
	{
		printf( "Failed to initialize!\n" );
	}


It says if init() does not return true print "failed to initialize", and it doesn't look like it ever actually called init(). I would be very thankful if someone could explain to me what exactly is going on in this piece of code.
To call a function, you type the function name, followed by parenthesis. In functions which take no parameters, the parenthesis will be empty.

So, for example, to call your init function... you would do this:

init();

Here, you have the function name init. Followed by empty parenthesis to indicating you want to call the function.

The semicolon ; exists only to mark the end of a statement, and does not have anything to do with the function call itself.

If you look at the code you posted... it is doing that on line 4:

if( !init() )

Inside that if statement, it is calling init()
Note there is no semicolon here because it is not the end of the statement. This statement does more than just calling init.

The bang ! is a logical NOT operator. It basically switches true<->false. So if init() returns true, the ! will make it false, and vice versa.

So.... basically... all of these are the same:
1
2
3
4
5
6
7
8
9
10
if( !init() )

// ... is the same as ...

if( init() == false )

// ... is the same as ...

bool temp = init();
if( temp == false )
So when it evaluates if init() returns false it also calls init() at the same time? I think I get it now.
Last edited on
So when it evaluates if init() returns false it also calls init() at the same time?


Yes.

For init to return anything it has to be called.

"returning" is a function's output. It can't have any output if you don't call it. ;)
So when it evaluates if init() returns false it also calls init() at the same time?

How could init return anything, if it wasn't being called?
Topic archived. No new replies allowed.