Not sure I'm understanding the proper use of functions

I'm not sure I understand how to use functions. If someone could take a look at this and tell me if I wrote it correctly, and offer some quick help if I didn't I'd really appreciate it. The program itself appears to work correctly, but I'm supposed to be using return value functions for the calculations.

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
75
76
77
/*************
Program:	Asg10.cpp	
Author: 	Alan P. Matie                      
Date:       01 Apr 2009
Description: 
	A program to compute and print your bill (with your name on it) if you order a pizza,
	pop, and chicken wings. Sales tax is 10% (since pizza is classified as junk food!). 
	Output all input values and calculated values. You friend Tad gives you a 10% discount 
	on everything but the chicken wings.
New Concepts: 
Challenges:
	Introduce Return Value Functions 
Last Modified: 07 Apr 2009
**************/

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

// Declare constants
const double DISCOUNT = 0.10; 
const double SALESTAX = 0.10;


// Function Prototypes
double totAmt(double pizza_cost, double pop_cost, double chicken_wing_cost);
double discAmt(double pizza_cost, double pop_cost, double DISCOUNT);
double subtotal(double totAmt, double disAmt);
double taxAmt(double subtotal, double SALESTAX);
double dueAmt(double subtotal, double taxAmt);


int main()
{
	// Declare variables below here
	string name;
	double pizza_cost, pop_cost, chicken_wing_cost, totAmt;
	double discAmt, taxAmt, dueAmt, subtotal;
	
	// Initialization Section for real number output. DO NOT MOVE!
	cout <<setiosflags(ios::fixed | ios::showpoint);
	cout <<setprecision(2);
	// Begin your "main processing" below here
	cout << "Please enter your name. Enter end to stop." <<endl;
	cin >> name;
	while (name != "end")
	{
		cout << "Please enter the cost of the pizza: $";
		cin >> pizza_cost;
		cout << "Please enter the cost of the pop: $";
		cin >> pop_cost;
		cout << "Please enter the cost of the chicken wings: $";
		cin >> chicken_wing_cost;
		totAmt = (pizza_cost + pop_cost + chicken_wing_cost);
		discAmt = ((pizza_cost + pop_cost) * DISCOUNT);
		subtotal = (totAmt - discAmt);
		taxAmt = (subtotal * SALESTAX);
		dueAmt = (subtotal + taxAmt);
		cout << "Bill for: " <<name <<endl;
		cout << "Total cost of food for: "<<name <<" $"<<totAmt <<endl;
		cout << "    Less discount on pizza and pop: $"<<discAmt <<endl;
		cout << "    Total less discount: $"<<subtotal <<endl;
		cout << "    Plus tax: $"<<taxAmt <<endl;
		cout << "TOTAL DUE: $"<<dueAmt <<endl;
		cout << "Please enter your name. Enter end to stop." <<endl;
		cin >> name;
	}



	
	return 0;
}

// function definitions below here 
I don't know what compiler you're using, but using Visual Studio 2008 you'll get an error like this:
error C2659: '=' : function as left operand

Functions are not variables that hold values, which is why you cannot assign to them something like an integer or double. Functions are in essence a collected series of statements under a common name.

Here's the simplest example of a function I know, with example on usage:
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
#include <iostream>

using namespace std;

// Function Prototype.
int add(int x, int y);

int main()
{
	int firstNumber;
	int secondNumber;

	cout << "Please enter two whole numbers separated by pressing the ENTER key." << endl;
	cin >> firstNumber;
	cin >> secondNumber;

	cout << "\nThe sum of the two number is " << add(firstNumber, secondNumber) << "." << endl;

	return 0;
}

// Function Definition
int add(int x, int y)
{ 
	return (x+y);
}

Here is a tutorial on functions: http://www.cplusplus.com/doc/tutorial/functions/
Ok, I read the tutorials, and then read the chapter in my textbook again, and compared them to the Labs my Instructor gave us, and I think I figured it out.
The following code runs, and so far it appears all of the calculations are correct, would someone look and tell me if I got this correct? Also this seems like an awful lot of work for some simple math; is there an easier way, or will this be more useful in future coding?


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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*************
Program:	Asg10.cpp	
Author: 	Alan P. Matie                      
Date:       01 Apr 2009
Description: 
	A program to compute and print your bill (with your name on it) if you order a pizza,
	pop, and chicken wings. Sales tax is 10% (since pizza is classified as junk food!). 
	Output all input values and calculated values. You friend Tad gives you a 10% discount 
	on everything but the chicken wings.
New Concepts: 
Challenges:
	Introduce Return Value Functions 
Last Modified: 07 Apr 2009
**************/

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

// Declare constants
const double DISCOUNT = 0.10; 
const double SALESTAX = 0.10;


// Function Prototypes
double CalctotAmt(double pizza_cost, double pop_cost, double chicken_wing_cost);
double CalcdiscAmt(double pizza_cost, double pop_cost, double DISCOUNT);
double Calcsubtotal(double totAmt, double discAmt);
double CalctaxAmt(double subtotal, double SALESTAX);
double CalcdueAmt(double subtotal, double taxAmt);


int main()
{
	// Declare variables below here
	string name;
	double pizza_cost, pop_cost, chicken_wing_cost, totAmt;
	double discAmt, taxAmt, dueAmt, subtotal;
	
	// Initialization Section for real number output. DO NOT MOVE!
	cout <<setiosflags(ios::fixed | ios::showpoint);
	cout <<setprecision(2);
	// Begin your "main processing" below here
	cout << "Please enter your name. Enter end to stop." <<endl;
	cin >> name;
	while (name != "end")
	{
		cout << "Please enter the cost of the pizza: $";
		cin >> pizza_cost;
		cout << "Please enter the cost of the pop: $";
		cin >> pop_cost;
		cout << "Please enter the cost of the chicken wings: $";
		cin >> chicken_wing_cost;
		cout << "Bill for: " <<name <<endl;
		totAmt = CalctotAmt(pizza_cost, pop_cost, chicken_wing_cost);
		discAmt = CalcdiscAmt(pizza_cost, pop_cost, DISCOUNT);
		subtotal = Calcsubtotal(totAmt, discAmt);
		taxAmt = CalctaxAmt(subtotal, SALESTAX);
		dueAmt = CalcdueAmt(subtotal, taxAmt);
		cout << "Total cost of food for: "<<name <<" $"<<totAmt <<endl;
		cout << "    Less discount on pizza and pop: $"<<discAmt <<endl;
		cout << "    Total less discount: $"<<subtotal <<endl;
		cout << "    Plus tax: $"<<taxAmt <<endl;
		cout << "TOTAL DUE: $"<<dueAmt <<endl;
		cout << "Please enter your name. Enter end to stop." <<endl;
		cin >> name;
	}



	
	return 0;
}


// function definitions below here
double CalctotAmt(double pizza_cost, double pop_cost, double chicken_wing_cost)
{
	double totAmt;
	totAmt = (pizza_cost + pop_cost + chicken_wing_cost);
	return totAmt;
}

double CalcdiscAmt(double pizza_cost, double pop_cost, double DISCOUNT)
{
	double discAmt;
	discAmt = ((pizza_cost + pop_cost) * DISCOUNT);
	return discAmt;
}

double Calcsubtotal(double totAmt, double discAmt)
{
	double subtotal;
	subtotal = (totAmt - discAmt);
	return subtotal;
}

double CalctaxAmt(double subtotal, double SALESTAX)
{
	double taxAmt;
	taxAmt = (subtotal * SALESTAX);
	return taxAmt;
}

double CalcdueAmt(double subtotal, double taxAmt)
{
	double dueAmt;
	dueAmt = (subtotal + taxAmt);
	return dueAmt;
}


edited typos
Last edited on
Of course, being the functions so simple, they aren't that useful in this example but functions are essential in programming.
Your code seems OK but if you can save some lines from that code, all of your functions have 3 lines of code but they can be reduced to one
eg:
1
2
3
4
double CalctotAmt(double pizza_cost, double pop_cost, double chicken_wing_cost)
{
	return pizza_cost + pop_cost + chicken_wing_cost;
}
That will make for a lot less typing, and I found that example in the textbook, after I read this.
Thanks for the help ,and I am soo glad I found this website, my Instructor is very helpful, but she doesn't answer questions outside of school hours very often. Not that I blame her, everyone needs time away.
fyi MatieA, you should take your name and info off the top of your program. some professors may be watching their students for academic misconduct (or something of the sort). don't give them a reason to slam you.
Topic archived. No new replies allowed.