The point of return?

Hey guys!

I´m new to C++ and don´t really understand the point of return 0 and other return functions.
Can some 1 please explain what it is? and what to use it for? In a very simple
way if possible.

Thank you very much.

closed account (N36fSL3A)
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.
Last edited on
it returns something..... it is used for calculation of muitiple variables
like this

int add(int x,int y)
{
return x+y
}


the above code returns the addition of two variables (x and y)
I am a beginner just like you and i struggled with the return statement as well. For my understand when "return" is execute in a function the function immediately terminates. Return 0 means just end the function with 0 value (nothing). Also return could return a value for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main 
{
 int a, y ,  x;
 
cin >> y >> x;

if (y > x)
a = 10;
else 
a = 5;
return a ; // end with "a" value and finish the program because is inside the main function
}

//  Second example return finish as a value 

 int example2( int y , int  x)
{
int a;
if (y > x)
a = 10;
else 
a = 5;
return a ; // end with "a" value and finish the function but program will keep running
}
The return key word does what it says on the tin, it returns a value.

Lets say you write a small program in main that adds 2 numbers together but then you decide to write a function "addNumbers(int x, int y)". You will have to declare the function with a return type as such,

int addNumbers(int x, int y);

Now your function MUST return an "int" type value. Since X and Y will be added together inside your function you can return the value of the sum this way. So back in main you might have something like this,

int result;
result = addNumbers(x, y);

If you tried to call the function by its self

addNumbers(x, y);

then you will get an error since the function is returning a value and you are not catching that value in a variable that matches its return type.

Remember that when you pass variables/values to a function then they are now outside the scope of where you called them from. This is why the return value exists.

However, functions with a return type "void" do not need a return type. They are void functions and return nothing.

I prob dribbled on for a bit there.

Kohana
Last edited on
Thanks everyone for the reply!
I understand it now :)
Topic archived. No new replies allowed.