Hi everyone so I am studying for a test tomorrow and I am doing a sample test and it's asking me to define the functions. What does defining a function mean and also what is really the purpose of functions (in the simplest ways please). here are some of the functions it's asking me to define so you can understand what it's about.
Implementation Notes:
Define the following functions:
printMenu(): To print the menu of the pizza parlor, as shown in the Expected Sample Run section.
inputStyle(): To read, validate and return the style of pizza: New York (N/n), Deep dish (D/d) or Stuffed Crust (S/s).
Your function should read the pizza style as a character and return it as a character.
It should handle both lower and upper case input.
inputToppings(): To read, validate and return the number of toppings, minimum 0, maximum 4.
inputPizzaDetails(): To pass by reference, the style of pizza and number of toppings for the current order. It should in return call inputStyle() and inputToppings() functions.
anotherPizza(): To read, validate and return whether the user wants to purchase another pizza (p/P) or quit (q/Q).
Your function should read the response (p/P/q/Q) as a character and return it as a character.
It should handle both lower and upper case input.
calculateCost(): To calculate the cost of a pizza:
It should obtain the style of pizza and the number of toppings as parameters from the caller.
It should return the cost to the caller.
A definition of a function is the code within the function code block.
Essentially what you want the function to do.
So if someone says define the function they mean write the functions code.
Not to be confused with a declaration which states this function exists in the code somewhere.
what is really the purpose of functions?
There are many reasons why functions are used. Some of these include.
1) To break up code to make it easier to understand as long block of code are confusing.
2) Eliminates redundant code (code that does the same thing multiple times in the program). So instead of writing code that calculates the sum of two numbers for 5 different calculations, five times. you write it once and send the numbers into the function, then return the value.
3) Simplifies code. calling the function SumTwoNumbers(); you immediate know what its purpose does and do not have to worry about the inner workings.