Need help with functions

Hello :) I need help.

I am having so much trouble with functions. Declaring them and calling them.
I have a program I have to do that I have to declare and call functions of an existing program I have written already.

I'm not asking anyone to do my work for me but I desperately need an example so I can work with it myself. My brain is just blanking out on this and I'm stuck. Hopefully I can get a "Eureka" moment.

These are the functions I'm supposed to declare.
inputNumber - takes a string representing a prompt as an argument, displays the prompt, reads an int value from the console, then returns that value.
 calculateCalories - takes a double representing the burn rate, an int representing the weight, and an int representing the number of minutes, and returns the result of multiplying the three numbers together.
 displayHeadings - takes no arguments, displays the headings for the table to the console, and does not return a value.
 displayLine - takes a string representing an activity, an int representing hours, an int representing minutes, and a double representing calories, displays a formatted line of output to the console, and does not return a value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()

{

	cout << setprecision(3) << fixed;
	cout << "Welcome to Name's workout calculator!" << endl;
	cout << endl;

	cout << "Please enter your name: " << endl;
	string name;
	getline(cin, name);

	cout << "Please enter your weight: " << endl;
	unsigned int weight;
	cin >> weight;
	cin.ignore(100, '\n');

	cout << "Please enter the minutes spent playing badminton: " << endl;
	unsigned int minsbadminton;
	cin >> minsbadminton;
	cin.ignore(100, '\n');

	cout << "Please enter the minutes spent running: " << endl;
	unsigned int minsrunning;
	cin >> minsrunning;
	cin.ignore(100, '\n');

	cout << "Please enter the minutes spent walking: " << endl;
	unsigned int minswalking;
	cin >> minswalking;
	cin.ignore(100, '\n');

	cout << "Please enter the minutes spent lifting weights: " << endl;
	unsigned int minsweights;
	cin >> minsweights;
	cin.ignore(100, '\n');
	cout << endl;

	int totalmins = minsbadminton + minsrunning + minswalking + minsweights;
	int totalhours = totalmins / 60;
	int minutes = totalhours % 60;

	const double BADMINTON = 0.044;
	const double RUNNING = 0.087;
	const double WALKING = 0.036;
	const double WEIGHTS = 0.042;

	double TTLCALBADMINTON = BADMINTON * weight * totalmins;
	double TTLCALRUNNING = RUNNING * weight * totalmins;
	double TTLCALWALKING = WALKING * weight * totalmins;
	double TTLCALWEIGHTS = WEIGHTS * weight * totalmins;
	double TTLCALSBURNED = TTLCALBADMINTON + TTLCALRUNNING + TTLCALWALKING + TTLCALWEIGHTS;

	cout << "Here are the results for " << name << ":" << endl;
	cout << endl;
	cout << "Activity" << left << setw(10) << right << "Time" << setw(15) << right << "Calories" << setw(21) << endl;
	cout << "---------------------------------" << endl;
	cout << "Badminton " << left << setw(6) << right << minsbadminton / 60 << ":" << minsbadminton % 60 << setw(14) << right << TTLCALBADMINTON << endl;
	cout << "Running " << left << setw(8) << right << minsrunning / 60 << ":" << minsrunning % 60 << setw(14) << right << TTLCALRUNNING << endl;
	cout << "Walking " << left << setw(8) << right << minswalking / 60 << ":" << minswalking % 60 << setw(14) << right << TTLCALWALKING << endl;
	cout << "Weights " << left << setw(8) << right << minsweights / 60 << ":" << minsweights % 60 << setw(14) << right << TTLCALWEIGHTS << endl;
	cout << "---------------------------------" << endl;
	cout << "Totals " << left << setw(9) << right << totalhours << ":" << minutes << setprecision(2) << setw(15) << right << TTLCALSBURNED << endl;

	system("PAUSE");
	return 0;

}
Have you read this: http://www.cplusplus.com/doc/tutorial/functions/

A function declaration looks like this:
1
2
3
4
5
<return type> name of function <arguments to the function>
{
	<stuff to be executed when the function is called>
	return <expression>
}

Calling a function looks like this:
 
nameoffunction(arguments to function if required);
Here's a bare-bones sample that you can compile and run. You can use this as an example to figure out how your other functions should work, too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

//inputNumber - takes a string representing a prompt as an argument,
//displays the prompt, reads an int value from the console,
//then returns that value.

// function DECLARATION
int inputNumber(std::string prompt);

int main()
{
    //function CALL
    int aNumber = inputNumber("Give me a number: ");

    std::cout << "I called the inputNumber function and it returned " << aNumber << std::endl;
    return 0;
}

//function DEFINITION
int inputNumber(std::string prompt)
{
    int usrInput;
    std::cout << prompt;
    std::cin >> usrInput;
    return usrInput;
}
I looked at the functions tutorials and I get a bit of it. I understand the functions that do not return a value. Still a little fuzzy on how I would work my program around them.

Is this what the part inputNumber should look like?

Does this only work for just one thing? Or can I make it work for a series like the user's weight and mins spent in each activity?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>

using namespace std;

//inputNumber - takes a string representing a prompt as an argument,
//displays the prompt, reads an int value from the console,
//then returns that value.

// function DECLARATION
int inputNumber(string& weight, string& minsbadminton, string& minsrunning, string& minswalking, string& minsweights);

int main()
{
    //function CALL
    int aNumber = inputNumber("Please enter your weight: ");

    cout << aNumber << endl;
    return 0;
}

//function DEFINITION
int inputNumber(string prompt)
{
    int usrInput;
    cout << prompt;
    cin >> usrInput;
    return usrInput;
}


I know I'm not getting this right.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

// Declaring the function displayHeading
void displayHeading()
{

	cout << "Activity" << left << setw(10) << right << "Time" << setw(15) << right << "Calories" << setw(21) << endl;
	cout << "---------------------------------" << endl;

}
// Declaring displayLine
void displayLine(int& minutes, int& hours)
{
	minutes = userInput;
	int = minutes / 60;
	int = hours % 60;
}

int main()

{

	cout << setprecision(3) << fixed;
	cout << "Welcome to Name's workout calculator!" << endl;
	cout << endl;

	cout << "Please enter your name: " << endl;
	string name;
	getline(cin, name);

	cout << "Please enter your weight: " << endl;
	unsigned int weight;
	cin >> weight;

	cout << "Please enter the minutes spent playing badminton: " << endl;
	unsigned int minsbadminton;
	cin >> minsbadminton;

	cout << "Please enter the minutes spent running: " << endl;
	unsigned int minsrunning;
	cin >> minsrunning;

	cout << "Please enter the minutes spent walking: " << endl;
	unsigned int minswalking;
	cin >> minswalking;

	cout << "Please enter the minutes spent lifting weights: " << endl;
	unsigned int minsweights;
	cin >> minsweights;
	
	cout << endl;

	cout << "Here are the results for " << name << ":" << endl;
	cout << endl;
// Calling the function displayHeading
	displayHeading();
	int minutes, hours;
//Calling the function displayLine
	displayLine(minutes, hours);
	cout << "Badminton " << left << setw(6) << right << minutes << ":" << minsbadminton % 60 << setw(14) << right << endl;
	cout << "Running " << left << setw(8) << right << minutes << ":" << minsrunning % 60 << setw(14) << right << endl;
	cout << "Walking " << left << setw(8) << right << minutes << ":" << minswalking % 60 << setw(14) << right << endl;
	cout << "Weights " << left << setw(8) << right << minutes << ":" << minsweights % 60 << setw(14) << right << endl;
	cout << "---------------------------------" << endl;
	cout << "Totals " << left << setw(9) << right << hours << ":" << minutes << setprecision(2) << setw(15) << right << endl;

	system("PAUSE");
	return 0;

}


So here's what I've tried so far. Doesn't compile because I can't figure out the calculateCalories part.

1
2
3
4
5
6
double calculateCalories (double burn_rate, int weight, int minutes)
{
double calories;
calories = burn_rate * weight * minutes;
return calories;
}
Is there a specific reason why use a function prototype? It would be easier to read if you used a normal function.

From the code example above, I assume that you are trying to make a function that reads the users weight and uses that as a return value? One thing that isn't clear to me is the prompt variable. What is it meant to do? As for entering the user's weight and returning the value, the way I would do it is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int weight(void){
	cout << "Please enter your weight..." << endl;
	int userWeight;
	cin >> userWeight;
	return userWeight;
}

int main(){
	cout << "Your weight is: " << weight() << endl;
	return 0;
}



Ah, I'm only using this way because that's in my instructions. :(
I forgot to add how it's supposed to be called.

Call inputNumber when getting the weight and the number of minutes for each activity. You will need to store the return value in a variable.
Call calculateCalories when calculating the calories for each activity. You will need to store the return value in a variable.
Call displayHeadings when displaying the headings for the table.
Call displayLine for each line of data in the table including the line with the totals.

It's just very difficult for me I don't understand. I get the displayHeadings and displayLine, well sort of. But it's the calculations from what I already have that I'm having so much trouble with. Especially counting user input.

Thank you very much for helping me along by the way.
A function is useful when you want to do the same thing a lot. You don't want to copy and paste code everywhere, because if the logic changes you have to change it in all the places you pasted the code. There's a good chance you'll miss a change somewhere and you'll introduce bugs.

In this case, the thing you want to do a lot is "show the user a prompt and read a number". In this case, the prompt can be different things, so that's why the prompt is a parameter. That let's you change the prompt that is printed when you call the function, but the rest of the behavior (read an integer and return it) is the same. See the code below and notice how the inputNumber function is used to populate all the different variables. The only thing different about each one is the prompt I show the user:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <string>

//inputNumber - takes a string representing a prompt as an argument,
//displays the prompt, reads an int value from the console,
//then returns that value.

// function DECLARATION
int inputNumber(std::string prompt);

int main()
{
    //function CALLS
    int minsBadminton = inputNumber("How many minutes did you play badminton?: ");
    int minsRunning = inputNumber("How many minutes did you run?: ");
    int minsWalking = inputNumber("How many minutes did you walk?: ");
    int minsWeights = inputNumber("How many minutes did you lift weights?: ");

    std::cout << "You played badminton for " << minsBadminton << " minutes.\n";
    std::cout << "You ran for " << minsRunning << " minutes.\n";
    std::cout << "You walked for " << minsWalking << " minutes.\n";
    std::cout << "You lifted weights for " << minsWeights << " minutes.\n";

    return 0;
}

//function DEFINITION
int inputNumber(std::string prompt)
{
    int usrInput;
    std::cout << prompt;
    std::cin >> usrInput;
    return usrInput;
}
Sorry for the late reply. That's becoming a lot clearer to me now! I was stuck on making the prompt and parameter one thing. Thank you very much!
Topic archived. No new replies allowed.