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:
A sample run from my program is show here. I have underlined things that the user inputs for clarity.
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 doesnt 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
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.
An abbreviated main function would then look like this:
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.