returning a 1 throughout the code

I am currently going through SDL tutorials by lazyfoo on lazyfoo.net and I was wondering if someone could explain something for me. The tutorials often use code like:

1
2
3
4
5
6
    
    //Initialize
    if ( load_files() == false )
    {
        return 1;
    }


to initialize functions in order to return a 1 if something goes wrong. I was wondering how you find out which function returns a 1 if the program doesn't compile correctly. I am currently using visual studio 2010.
You could use an if statement.
1
2
if(someFunction() == 1)
cout<<"someFunc returned 1"<<endl;

the function will be called, and the value returned will by compared, which will be either true or false.
I was wondering how you find out which function returns a 1 if the program doesn't compile correctly.

The return value is used when the program is running, they can help determine logical errors or issues, not compiler errors.
ahhh I see, thanks ericeps
closed account (4Gb4jE8b)
you could also return other numbers, such as -1 or 2 or really anything I THINK (i may not be correct) so that each function returns a different error code
The value you can return is based on the function declaration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool myBoolFunct()
{
//some code
//this function can return either 1 or 0;
return 1;
}

int myIntFunc()
{
//this function can return int integers
return -44;
}

char myCharFunc()
{
//this returns char
return 'i';
}

unsigned int myUnsignedIntFunc()
{
//this function returns 'unsigned int'
return 108;
}
Last edited on
closed account (4Gb4jE8b)
oh silly me, i knew that too :P well my sincere apologies for misleading you. Thanks eric!

note int main acts as an int function (like it's so special :P), so any for loops or if statements can return any error code in the scope of int, or return 0, which is common for showing success.

note 2, bool can also return true or false, such as if something exists or is equal to something else.
Actually you are not restricted to just the primitive type. You can have pointer to primitive type too but this can lead to memory leak if not taken care properly so usually you don't see such declaration much.

1
2
3
4
5
6
7
bool* myBoolFunct()

int* myIntFunc()

char* myCharFunc() // this one quite commonly seen so be careful !!!

unsigned int* myUnsignedIntFunc()
Topic archived. No new replies allowed.