# Thread: Question with Functions

1. ## Question with Functions

I'm working on a program that needs to calculate the total cost of tuition.

Enter amount of credits -- multiply credits by 100.00 -- add 20.00 as student activity fee -- print final number.

I can get the program to work the following way:

Code:
```#include <iostream>
using namespace std;

int main()
{
int iCredits;
double fTotal;
cout << "Please enter a the number of credits you are taking this semester: ";
cin >> iCredits;

cout << "\$ " << iCredits * 100 + 20 << endl;

system("PAUSE");
return 0;
}```
When I try to break down the processes in functions I get some number with E in it.

Can anyone help me with a suggestion to break down the above program using multiple functions? One to compute credits * 100.00, one to add the extra 20.00 and finally one to print out the final total.

2. What did you try?

3. Code:
```#include <iostream>
using namespace std;
// pass the parameter by reference, so that
// the changes will be remembered
void input(int& iCredits);
int main()
{
int iCredits;
double fTotal;
input(iCredits);

cout << "\$ " << iCredits * 100 + 20 << endl;

system("PAUSE");
return 0;
}

void input(int& iCredits)
{
cout << "Please enter a the number of credits you are taking this semester: ";
cin >> iCredits;
}```
Continue with the rest

4. Originally Posted by std10093
Code:
```#include <iostream>
using namespace std;
// pass the parameter by reference, so that
// the changes will be remembered
void input(int& iCredits);
int main()
{
int iCredits;
double fTotal;
input(iCredits);

cout << "\$ " << iCredits * 100 + 20 << endl;

system("PAUSE");
return 0;
}

void input(int& iCredits)
{
cout << "Please enter a the number of credits you are taking this semester: ";
cin >> iCredits;
}```
Continue with the rest

Thanks, I went with this:

Code:
```#include <iostream>
using namespace std;

double dCalcTotal ();
double PrintTotal (double dTotal);
int iCredits;

int main()
{

cout << "Please enter a the number of credits you are taking this semester: ";
cin >> iCredits;

PrintTotal (dCalcTotal());

system("PAUSE");
return 0;
}

double dCalcTotal ()
{
double dTotal;
dTotal =iCredits * 100.00 + 20.00;
return dTotal;
}

double PrintTotal (double dTotal)
{
cout << "\$ " << dTotal << endl;
return 0;
}```

5. You are welcome. You said that you wanted the multiplication to be done in one function and the addition in another one. In your implementation you do both in one function. This is ok, but if the requirements are to split these two operations into two functions, then you should do it. It is easy.

When a function does not return something, like in your print one, you write void. So, your print function should be like this
Code:
```void rintTotal (double dTotal)
{
cout << "\$ " << dTotal << endl;
}```
I do not like that you put iCredits as a global variable. Now your program is very small, but as the programs get bigger and bigger and more programmers work on the same projects, global variables, can really be problematic and prone to conflicts (variable shadowing).

Declare it in the main and pass it as a parameter, like this
Code:
```double dCalcTotal (int iCredtis)
{
double dTotal;
dTotal =iCredits * 100.00 + 20.00;
return dTotal;
}```
Don't forget to change the prototype and call the function with the iCredits as a parameter in the main.

I really like what you did in line 16.

Also, notice, that next time you start a thread, it would be nice to post your try first

Popular pages Recent additions