Assignment 1 Clarification and Return Types

As many of you have noticed, completing assignment 1 is not as straightforward as it looks. The purpose of this assignment was to get a job done within the constraints that I outlined. Within those constraints it is ok to bend the rules. This was definitely necessary for this assignment given what you have learned so far. In addition, however, you need to break one rule. You must ask for the number of people at the party in main. The rest of main can be just functions. Therefore my main looks something like this:

int main(void)
{
    int people;
    cout<<"Enter the number of people: "; //prompt for number of people
    cin>>people; //store number of people
    soda(people); //calculate soda needed and assign someone to bring it
    pizza(people); //calculate pizza needed and assign someone to bring it
    cake(people); //calculate cake needed and assign someone to bring it
    return 0;
}

A sample run from my program is show here. I have underlined things that the user inputs for clarity.

Enter the number of people: 10
The number of sodas needed is 2.5
The cost of the sodas is $2.475
Cost per person is $0.2475
Enter the person who is going to bring the sodas: Kim
Tell Kim to bring along $2.475 to cover the cost.
Enter the pizza radius in inches: 9
The number of pizzas needed is 1.17952
The cost of the pizzas is $10.4167
Cost per person is $1.04167
Enter the person who is going to bring the pizzas: Linwood
Tell Linwood to bring along $10.4167 to cover the cost.
Cake length needs to be 13
Cake cost is $8.33333
Cake cost per person is $0.833333
Enter the person who is going to bring the cake: Michal
Tell Michal to bring along $8.33333 to cover the cost.
Press any key to continue

You could actually get away with nothing but functions in main, but this would require asking for the number of people in each of the other functions. While this may fit better in the constraints I defined, it is certainly less efficient programming. Additionally, you could do other things like print out total cost per person by recalculating each of the individual costs in another function, but there doesn’t seem to be a real need for this.

Returning values from functions

What would make all of this easier would be if the functions that we are using could return values. For example we could easily calculate a total cost if each of the item functions (pizza, soda and cake) sent back their individual costs to main where we could total them up. This is actually pretty easy to do. In place of the void out in front of the function name we can put a return type that indicates what kind of value the function will be sending back. For example if we want to send back a cost in dollars and cents this will likely be a float. The only other thing you need to do is at the end of the function you must issue a statement that says "return" and the value that you want returned. When you put it all together, it looks like this

float soda(int people)
{
    do stuff here
    return cost;
}

You do need to do one more thing for this to work. When a function has a return value, you should assign this value to a variable. For example, in main, instead of just calling soda() you should say sodacost=soda().

This is a little confusing, so I will make a new function called partyfavors that illustrates how to do this. Let's assume that it will cost $.50 per person for party favors. The partyfavors function might look like this.

float partyfavors(int people)
{
    float cost;
    cost=.50*people;
    cout<<"The cost is "<<cost<<endl;
    return cost;
}

An abbreviated main function would then look like this:

int main(void)
{
    float favorscost;
    other stuff here
    favorscost=partyfavors();
}

See if you can use this concept to change your assignment 1 program so that the number of people is input in a separate function. Also modify it so that it can calculate a total cost of the party.

Back to Home Page House3.wmf (25540 bytes)