How to call user defined functions in C++?

When you declare the prototype of the function you can omit the parameter but in the implementation you must place it.

change:

void square(double)
{
    double sqrSide = 0;
    double sqrTot = 0;
    double sqrArea;

    sqrArea = sqrSide * 4;

    //get the total area and store it as a variable
    sqrTot += sqrArea;

    if (sqrTot > 0) {
        cout << "Shape: Square" << endl;
        cout << "Side: " << sqrSide << endl;
        cout << "Area: " << sqrTot << endl;
    }


}

to:

void square(double sqrSide)
{
    double sqrTot = 0;
    double sqrArea;

    sqrArea = sqrSide * 4;

    //get the total area and store it as a variable
    sqrTot += sqrArea;

    if (sqrTot > 0) {
        cout << "Shape: Square" << endl;
        cout << "Side: " << sqrSide << endl;
        cout << "Area: " << sqrTot << endl;
    }


}

and also change:

case 1:
                cout << "What is the length of the side: ";
                cin >> sqrSide;

                square(sqrSide);

                if (sqrTot > 0) {
                    cout << "Shape: Square" << endl;
                    cout << "Side: " << sqrSide << endl;
                    cout << "Area: " << sqrTot << endl;
                }

                cout << endl;

                system("pause");

                break;

to:

case 1:
                cout << "What is the length of the side: ";
                cin >> sqrSide;

                square(sqrSide);

                system("pause");

                break;

Leave a Comment