0

I am beginner at C++ (or all coding in general). That being said, I am trying to understand what return 0/return 1/return -1 defines outside of the main() function.

What I do know about Return values is that it allows one to end a statement and get a "return" integer value. I was told (by my professor) that return 0 is like a "achieved" status. Telling the user that everything is working fine.

However what is confusing me is the code below. Inside "int checkwin()" function it states that if a player wins, then the code will input "return 1". If no one wins, then "return 0". And the last one I don't understand completely (assuming it means keep going) because again, I haven't figured the definitions for return 0/1/-1.

#include <iostream>

using namespace std;

char square[10] {'0','1','2','3','4','5','6','7','8','9'};

int checkwin();
void board();

int main()

{ // main function OPEN

    int player;
    int i;
    int choice;

    char mark;

    do { // do while loop open

            board();

            player=(player%2)?1:2;

            cout << "Player " << player << ", enter a number: ";
            cin >> choice;

            mark =(player ==1) ? 'X' : 'O';

            if(choice==1 && square[1]=='1')

                    square[1] = mark;

            else if(choice==2 && square[2]=='2')

                    square[2] = mark;

            else if(choice==3 && square[3]=='3')

                    square[3] = mark;

            else if(choice==4 && square[4]=='4')

                    square[4] = mark;

            else if(choice==5 && square[5]=='5')

                    square[5] = mark;

            else if(choice==6 && square[6]=='6')


            else if(choice==7 && square[7]=='7')

                    square[7] = mark;

            else if(choice==8 && square[8]=='8')

                    square[8] = mark;

            else if(choice==9 && square[9]=='9')

                    square[9] = mark;
            else
            { // else statment OPEN

                    cout << "That is an invalid choice" <<endl;

                    player--;
                    cin.ignore();
                    cin.get();
            } // else statement CLOSED

            i=checkwin();
            player ++;

    } // do while loop CLOSED
    while (i==-1);

            board();
    if(i==1)

            cout << "==> Player " << --player << "win";
    else
            cout << "==> Game Draw" ;

    cin.ignore();
    cin.get();

    return 0;

} // main function CLOSED



 //****************************************************************
//**********************checkwin function *************************
//*****************************************************************

int checkwin()

{ // checkwin function bracket OPEN

   if(square[1] == square[2] && square[2] == square[3])

            return 1;

    else if(square[4] == square[5] && square[5] == square [6] )

            return 1;

    else if(square[7] ==  square[8] && square[8] == square [9] )

            return 1;

    else if(square[1] == square[4] && square[4] == square[7])

            return 1;

    else if(square[2] == square[5] && square[5] == square[8])

            return 1;

    else if(square[3] == square[6] && square[6] == square[9])

            return 1;

    else if(square[1] == square[5] && square[5] == square[9])

            return 1;

    else if(square[3] == square[5] && square[5] == square[7])

            return 1;

    else if(square[1] != '1' && square[2] != '2' && square[3] != '3' && square[4] != '4'
            && square[5] != '5' && square[6] != '6' && square[7] != '7' && square[8] != '8'
            && square[9] != '9' )

            return 0;

    else
            return -1;



} // checkwin function bracekt CLOSED

//*******************************************************************
//************************ board function ***************************
//*******************************************************************

void board()

{ // board function bracket OPEN

    system("cls");
    cout <<"\n\nTic Tac Toe\n\n";

    cout<< "Player 1 ( X )  -  Player 2 ( O )\n\n\n" ;

    cout<< "     |     |     " << endl;
    cout<< "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;

    cout<< "_____|_____|_____" << endl;
    cout<< "     |     |     " << endl;

    cout<< "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;

    cout<< "_____|_____|_____" << endl;
    cout<< "     |     |     " << endl;

    cout<< "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;

    cout<< "     |     |     "  << endl;

} // board function bracket CLOSED

Sorry if my question is a bit confusing or if I'm not being clear enough.

Fooji
  • 71
  • 7

3 Answers3

0

The value means whatever the author of the function wants it to mean. It's as simple as that.

Somebody wrote checkwin, and that person decided that it would return those values under those conditions. The people using the function can then expect to be able to interpret the function's result according to those unspoken rules.

I can write a function urgleburgle that returns the number 42 when the sky is blue and the number 98 when the sky is red. It returns the number 11111 when the sky is some other colour. And you, as the user of my function, will just have to accept that choice. There is no deeper meaning.

The author of the function should add documenting comments that explain what each possible return value means, so that you can understand the function and actually use it for meaningful stuff.

In your particular case, it looks like -1 is intended to mean "no result of the game yet; keep going", though since there are no comments of use, we can only guess. You should ask the author (e.g. your professor) to clarify what they intend each return value to mean, for this function.

main is a bit special because its user is the operating system (oversimplification warning!), so there has to be a predefined rule about what its return values mean, that the operating system can understand. This is often 0 for success and 1 for failure, though various other platform-specific conventions exist.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Interesting. I thought that return was always predefined in every function. This really cleared it up. Thanks man! – Fooji Feb 25 '19 at 00:03
  • As a point of interest, this is an example of "lies to children" going wrong. Your professor has, in good faith, tried to keep things simple for you by giving general rules/conventions about how you might want to design a function .... but neglected to inform you that the language doesn't make these rules. As a result: your confusion. As educators we all have a responsibility not to be misleading, even when we're trying to simplify. – Lightness Races in Orbit Feb 25 '19 at 00:07
  • Well to be fair, I am going a bit ahead from my class (all future assignments are up to view for the whole semester) just so I can understand the lectures to a greater degree. My professor hasn't discussed other functions besides of main() as of yet. I'm sure he will (hopefully) discuss return values in more detail when he lectures about it. – Fooji Feb 25 '19 at 00:28
  • Okay, then he was probably talking about `main` specifically. If you're learning on your own then you should do so from [a book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Lightness Races in Orbit Feb 25 '19 at 00:34
  • I would but my class doesn't have a book that we follow. But I guess I can find a c++ book off Amazon. – Fooji Feb 25 '19 at 00:39
  • @Fooji The text "a book" in my previous comment is a link – Lightness Races in Orbit Feb 25 '19 at 00:42
  • Oh I didn't even realize you sent me a link for books. Lots of different books. Well this saves me time from searching which book to buy. Thanks again. Really appreciate all your help – Fooji Feb 25 '19 at 01:00
0

The returned value of a function is part of its contract with the user. It should be clear from the documentation of the function what each return value means.

Usually, in unix based systems, returning 0 from the main of the program indicates a successful execution. However, inner function can, and usually do, not follow this convention.

In this code, returning -1 means that the game did not finish yet (keep executing the do-while loop). Returning 1 means that the game ended and there is a win. While, returning 0 means the game ended in a draw.

LmSNe
  • 143
  • 1
  • 9
0

-1 in checkwin means that not all board fields are filled and no one wins at the moment, so game is not yet finished

1) at initial position, any square[i] != X or O

char square[10] {'0','1','2','3','4','5','6','7','8','9'}; or, equivalently,

square[0] = '0'; // dummy value -- not used at all
square[1] = '1';
square[2] = '2';
square[3] = '3';
square[4] = '4';
square[5] = '5';
square[6] = '6';
square[7] = '7';
square[8] = '8';
square[9] = '9';

2) they place mars on board mark =(player ==1) ? 'X' : 'O';

3) game contunues, while state of the game is -1:

    do { // do while loop open
        ...
        i=checkwin();
        ...
    } while (i == -1)  

-1 means that not all board fields are filled and no one wins at the moment, so game is not yet finished

Alan Turing
  • 2,482
  • 17
  • 20
  • Ohhh okay. This really helps! I must have missed " i = checkwin()." I that would saved me an hour or so trying to figure out the code haha. Thanks man, appreciate you breaking it down for me. – Fooji Feb 25 '19 at 00:21