Look. Lets say I have a function that initializes my program. It loads the library I'm using. The Initialize lib function returns an integer that tells if it went okay. If it does, it returns 0. If it has a problem, it returns 1.
We check what value it returns with if statements.
If it doesn't load right, we tell the program. Then, we handle what the program does if the library fails to initialize.
Now for our general initialization function, we return 1, indicating our library wont load.
1 2 3 4 5 6 7 8 9 10 11
|
int Init()
{
if(InitializeLib() == 1)
{
std::cout << "There was a problem with library initialization.\n";
// Code to handle program
return 1;
}
return 0;
}
|
Now, we add on to this. If the library wont load, we return 1. How about if the window wont set up, we return 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
int Init()
{
if(InitializeLib() == 1)
{
std::cout << "There was a problem with library initialization.\n";
// Code to handle program
return 1;
}
if(SetWindow() == 1)
{
std::cout << "The window failed to set up.\n";
return 2;
}
return 0;
}
|
Alright, that's one purpose for returning return. Let's say we programming a game. We have a vector that holds the number of players. Our class has a function that returns how many players there are.
1 2 3 4 5 6 7 8
|
class PlayerContainer
{
public:
unsigned int getNumPlayers() const{return players.size();}\
private:
std::vector<Player*> players;
};
|
Now in the main function, we learn how many players there are.
1 2 3 4 5 6
|
PlayerContainer pc;
int main()
{
unsigned int number_of_players = pc.getNumPlayers();
}
|
Those are what they are mainly used for.