Function Prototype - How to Program a Complete C++ Calculator

In this tutorial in our C++ course, we will learn how to create a complete C++ calculator as well as understand what function prototypes are and what they are for.

Programming a Calculator in C++

Let's now program a very functional and useful calculator using the knowledge we learned from C++:


The sum(), sub(), mult(), and divis() functions perform the sum, subtraction, multiplication, and division operations, respectively. They all get two float data and return a float result as well.

Except for the divis() function, as it needs to test if the denominator is different than 0.
If it is, returns the division correctly.
If not, returns a message saying that it is not possible to divide by 0, which is the correct one to do.

The menu() function is responsible for showing the possible mathematical operations that it can do.
The user types a value as an option.

If this option is 0, the program exits the DO WHILE looping from the menu() and terminates the program.

If you type in any other number, we ask for the two values ​​that he will want to calculate the operation and then go to SWITCH, where we select the operation correctly.

We pass the numbers entered by the user to the respective function, she returns the result and we display it.

Finally, the options menu is displayed again, in case the user wants to do another calculation.
See how our code looks:
#include <iostream>
using namespace std;

float sum(float a, float b)
{
    return a+b;
}

float sub(float a, float b)
{
    return a-b;
}

float mult(float a, float b)
{
    return a*b;
}

float divis(float a, float b)
{
    if(b!=0)
        return a/b;
    else
        cout<<"Can't divide by 0\n";
}

void menu()
{
    int op;
    float a, b;
    do{
        cout<<"0. Exit\n";
        cout<<"1. Sum\n";
        cout<<"2. Subtraction\n";
        cout<<"3. Multiplication\n";
        cout<<"4. Division\n";
        cin >> op;

        if(op){
            cout<<"\nFirst Number: ";
            cin >> a;

            cout<<"Secont number: ";
            cin >> b;

            switch(op){
                case 1:
                    cout<<"Sum: " << sum(a,b) << endl;
                    break;
                case 2:
                    cout<<"Sub: " << sub(a,b) << endl;
                    break;
                case 3:
                    cout<<"Mult: " << mult(a,b) << endl;
                    break;
                case 4:
                    if(b)
                        cout<<"Division: " << divis(a,b) << endl;
                    else
                        divis(a,b);
                    break;
                default:
                    cout<<"Invalid option\n";
            }
        }else
            cout<<"Exiting...\n";
        cout<<endl;
    }while(op);
}

int main()
{
    menu();
    return 0;
}
In the main() function we simply invoke the function responsible for displaying the menu.
And only.

See, we use functions, mathematical operations, IF and ELSE, SWITCH and DO WHILE.
Basically, all the knowledge we have studied so far in our C++ course.

Now do an exercise.

C++ function prototypes

Take the main() function, cut it from the code, and paste it up before the functions of the math operations. Now run your code.

It should give a 'sum() was not declared' error, saying that the sum() function has not been declared.
This is because the compiler reads the code from top to bottom.

When he enters menu() and calls sum(), he doesn't know what to do, because this function has not been declared yet, it is below menu(). Therefore, it is correct to first declare the functions of mathematical operations, and only then to invoke them in the menu().

Now imagine a more complex and much larger program, how that would get complicated.
We'd have to be careful what to declare before that, and the code before the main() main function would look gigantic.

The solution to this is to declare only the function prototypes up there.

The prototype is nothing more than the function header, with the data types it receives and the correct return, followed by a semicolon, without the braces pair and function code.

Here's how our calculator code looks, using the prototypes:
#include <iostream>
using namespace std;

void menu();
float sum(float a, float b);
float sub(float a, float b);
float mult(float a, float b);
float divis(float a, float b);

int main()
{
    menu();
    return 0;
}

void menu()
{
    int op;
    float a, b;
    do{
        cout<<"0. Exit\n";
        cout<<"1. Sum\n";
        cout<<"2. Subtraction\n";
        cout<<"3. Multiplication\n";
        cout<<"4. Division\n";
        cin >> op;

        if(op){
            cout<<"\nFirst Number: ";
            cin >> a;

            cout<<"Secont number: ";
            cin >> b;

            switch(op){
                case 1:
                    cout<<"Sum: " << sum(a,b) << endl;
                    break;
                case 2:
                    cout<<"Sub: " << sub(a,b) << endl;
                    break;
                case 3:
                    cout<<"Mult: " << mult(a,b) << endl;
                    break;
                case 4:
                    if(b)
                        cout<<"Division: " << divis(a,b) << endl;
                    else
                        divis(a,b);
                    break;
                default:
                    cout<<"Invalid option\n";
            }
        }else
            cout<<"Exiting...\n";
        cout<<endl;
    }while(op);
}
float sum(float a, float b) { return a+b; } float sub(float a, float b) { return a-b; } float mult(float a, float b) { return a*b; } float divis(float a, float b) { if(b!=0) return a/b; else cout<<"Can't divide by 0\n"; }
Much more beautiful and organized, don't you think?

Incidentally, the compiler only needs to know the return, the name of the function, how many and what kind of parameters the function has, nor even their name, you can declare like this too:
void menu();
float sum(float, float);
float sub(float, float);
float mult(float, float);
float divis(float, float);
We always recommend using function prototypes, making your code more organized and less susceptible to errors and problems.

No comments:

Post a Comment