Hello,

I do not understand the following parts in bold - how it works, what is is for..etc. I'm not following the book's explanation..

#include <iostream>
using namespace std;
[B]
double totalCost(int numberParameter, double priceParameter);[/B]


int main( )
{
    double price, bill;
    int number;

    cout << "Enter the number of items purchased: ";
    cin >> number;
    cout << "Enter the price per item $";
    cin >> price;

    bill = totalCost(number, price);

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << number << " items at "
         << "$" << price << " each.\n"
         << "Final bill, including tax, is $" << bill
         << endl;

    return 0;
}

[B]double totalCost(int numberParameter, double priceParameter)[/B]
{
    const double TAXRATE = 0.05; //5% sales tax
    double subtotal;

    subtotal = priceParameter * numberParameter;
    return (subtotal + subtotal*TAXRATE);
}

I'm also not sure why it's called "parameter" here. Like I said - basically, I can't figure out why it's there and what it exactly does. I would appreciate any help.
Thank you

Let's start with the second one - the one following the main() function - first. What you have there is a function definition, which gives a name to a function - totalCost() in this case - and declares the type of the value it returns - double - and the parameters of the function ( numberParameter and priceParameter ) which are names for variables that get passed to the function when it is called. They are called 'parameters' because, well, they are analogous to the parameters of a mathematical function - it's simply a traditional name.

Now, let's take a quick look at main() which in this case calls totalCost() like so:

bill = totalCost(number, price);

here, number and price are the arguments of the function - they give the values which are put into the parameters. Each time you call a function, you can give it different arguments. The result of the function is returned and stored in bill , in this case.

Now, there's just one problem with all of this: the definition of totalCost() comes after the definition of main() . How is main() supposed to know what the type and parameters of totalCost() are? That's where the function prototype at the top comes in - it gives a capsule summary of the function, letting main() (and any other functions) know that there is a function called totalCost() around here somewhere, and that it expects arguments of type int and double and that it returns a double back. It doesn't do anything, per se, but it gives information about totalCost() .

Now, you could very well just move totalCost() to the top of the program, above the main() function, and forget about the function prototype. So you may be wondering, why have prototypes at all? Well, the big advantage of them is that you can put the in a header file which you can include in the program, which would let multiple programs know about all of the functions you put into the header. As explained here, you don't want to have the functions themselves in the header; but the header files can hold prototypes for literally hundreds of functions and classes - including things like the << and >> operators for the iostream classes - in fact, a lot of things you probably think are part of C++ are actually in the libraries and headers rather than built in.

commented: Thanks +0

Hello,

I do not understand the following parts in bold - how it works, what is is for..etc. I'm not following the book's explanation..

It called a prototype. If you use the function (line 17) before you define the function (line 30) the compiler does not understand line 17. Therefore, you add the prototype to tell the compiler "when you see this function, it's used like this way -- it will be fully defined later".

It's like a trailer to a movie. Lets the compiler know what it's about.

I'm also not sure why it's called "parameter" here. Like I said - basically, I can't figure out why it's there and what it exactly does. I would appreciate any help.
Thank you

Why what's a parameter? Maybe it's called that because that's what they call it. Like why do they call it an airplane -- because that's what it's called.

Hello,

I do not understand the following parts in bold - how it works, what is is for..etc. I'm not following the book's explanation..

#include <iostream>
using namespace std;
[B]
double totalCost(int numberParameter, double priceParameter);[/B]


int main( )
{
    double price, bill;
    int number;

    cout << "Enter the number of items purchased: ";
    cin >> number;
    cout << "Enter the price per item $";
    cin >> price;

    bill = totalCost(number, price);

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    cout << number << " items at "
         << "$" << price << " each.\n"
         << "Final bill, including tax, is $" << bill
         << endl;

    return 0;
}

[B]double totalCost(int numberParameter, double priceParameter)[/B]
{
    const double TAXRATE = 0.05; //5% sales tax
    double subtotal;

    subtotal = priceParameter * numberParameter;
    return (subtotal + subtotal*TAXRATE);
}

I'm also not sure why it's called "parameter" here. Like I said - basically, I can't figure out why it's there and what it exactly does. I would appreciate any help.
Thank you

The two things you have in bold are related to each other, but for now, let's forget about the first one near the top and concentrate on the one near the bottom.

Now the one at the bottom, of course, is called a function (definition). It calculates something, like the total cost (I would assume : ). And this function takes two paramaters, one an int and the other a double. And they can be named anything you want, the names do not have to match the variables that are being passed. For example, you could do this:

bill = totalCost(number, price);

double totalCost(int x, double y)
{
    const double TAXRATE = 0.05; //5% sales tax
    double subtotal;

    subtotal = y * x;
    return (subtotal + subtotal*TAXRATE);
}

So there is no need for the names to match at all, but of course the data types (int and double) have to match. Int x simply takes on the value of number, and double y takes on the value of price. Later in the program, this same function could be called with different variables, for example:

bill = totalCost(quantity, rate);

double totalCost(int x, double y)
{
    const double TAXRATE = 0.05; //5% sales tax
    double subtotal;

    subtotal = y * x;
    return (subtotal + subtotal*TAXRATE);
}

So x would be given the value of quantity and y would get the value of rate. So there is no relation to the names, nor is there any reason for there to be, because numerical values are simply being passed, not names. So long as you pass an int where an int is required, and a double where a double is required, then there is no problem. And that brings us to the other line you have in bold at the top:

That line is called a function prototype. And it's purpose is so that the compiler has a record of what data types are used in the function, as well as how many parameters there are and in what order. Armed with that information, the compiler can check your work, to make sure that you aren't passing a char when it should be an int, or a int when it should be a double. So the function prototype is just information for the compiler so it can double check your work and flag an error if you use pass the wrong data types to the function.

In days gone by, you didn't have to have a prototype for a function - you could leave it out if you wished and the program would still compile just fine. However, the compiler would not be able to double check your use of the function, and if you made a mistake and passed an int in place of a double then that could cause your program to crash at run time, or else make for a very hard to track down bug! So, around 1999, when a new standard was put forth, it became required to use a function prototype or else the code would not compile - thus forcing you to permit the compiler to check your work.

However, there is something else that you can use in place of a function prototype, and that is the function definition itself (the one you have in bold at the bottom of your code). So if you moved your function definition from the bottom to the top in place of the function prototype, then you wouldn't need to have the prototype because the function definition itself has the same information, and thus the compiler could still see what is required and still check your work.

So, that information, whether provided by a prototype or the function definition itself, must be available in your code before the function is ever used. That's the point. Also, names are not required in a prototype, and the prototype above could just be written without naming the parameters like this: double totalCost(int, double); because that is the only information that the compiler needs to check your work. You can name the parameters if you want for your own sake, but the compiler doesn't care.

commented: Thanks +0

Thank you, everybody.

So, total cost basically consists of number and price, and that's why it's totalCost( int numberParameter..... ), and I could also name is just number and price, without the "Parameter"? I also don't understand why there's a double before total cost...
And why did they put the function definition after the return 0; ? Because the function call needs a return 0; ?

Won't let me edit it...

I know the double is probably for the value totalCost returns.. but is "bill" that double value?

....total cost basically consists of number and price,...

Actually, totalCost basically consists of an int and double, and you can pass any int value or double value into those (like number and price for example).

....and I could also name is just number and price, without the "Parameter"?....

Yes, and you could also name them x and y like I did, the names don't matter at all, any more than the names of any other variables in the program matter - they are just identifiers for the variables. Names can be descriptive for OUR sake, and should be to help the code be clearer as to the kind of use of the variable, but names have no effect on the function or action or type of the variable itself.

And why did they put the function definition after the return 0; ? Because the function call needs a return 0; ?

That return 0 belongs to another function and has nothing to do with the totalCost function. That return 0 belongs to the main() function, and it is before the closing brace of main().

I know the double is probably for the value totalCost returns.. but is "bill" that double value?

"bill" is a double value, as defined at the start of the program in main(), and any double variable could be used to "catch" the double return value from totalCost. But 'bill" is the double that gets the honors in this case.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.