Defining a function

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.

What does defining a function mean?


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.
Last edited on
Define just means to write the code for it. A quick Google search about functions in programming explains it better than I ever could so here you go... http://www.cs.utah.edu/~germain/PPS/Topics/functions.html

Good luck
ok thank you so much also how do I call for a function? and do i leave the inputs inside main empty until i want to call for them?
you call a function with the name of the function followed by ().

1
2
3
4
int number = 0;
int number2 = 1;

somefunction(number, number2); // Is a call to a function 


when you call a function you must include any parameters it requires between the parentheses, if no parameters are required don't insert anything.


and do i leave the inputs inside main empty until i want to call for them?


I do not understand what you mean by this.
Last edited on
Topic archived. No new replies allowed.