Practice Exercise: User-Defined Functions--Basics
Purpose and Objective: To enhance readability by reducing the complexity of the
function main, it is useful to be able to create and invoke a function.
The general form or syntax for a function is:
functionType functionName ( [formal parameter list] ) {
statements
[return expr;]
}
The syntax for a function call is:
[variable=] functionName([arguments--aka "actual parameter list"]);
The syntax for the formal parameter list is:
dataType identifier [, dataType identifier, ...]
The syntax for the argument(s) or actual parameter list is:
expression or variable[, expression or variable, ...]
return Statement syntax:
return [expr]; where expr is a variable, constant or expression.
0. Declare and initialize variables for a char named planChoice, an int named
minutes(e.g. 30) and doubles for costPerMinute(0.10), and minutesCharge(0.00),
and string named planDescription("Regular plan").
1. Define a void(does not return a value) function named printMenu that has no
formal parameters, and prints a menu of choices for a phone plan user such as
Daily Plan (enter 'd')
Minute Plan (enter 'm')
...
2. Invoke a defined function named printMenu which has no arguments and returns
no value. Follow this "prompt" with an input statement for planChoice.
3. Define a type double value-returning function named getMinutesCharge. This
function should have an int parameter for minutes, and a char for planChoice.
The statement body is to test planChoice for 'd', assign 0.15 to
costPerMinute, or when it is 'm' assign 0.20 to costPerMinute. Next return
the double value for minutesCharge by finding the product of minutes
and costPerMinute.
4. Assign to minutesCharge the value returned by calling getMinutesCharge()
passing values for minutes and planChoice.
5. Define a void function named printResults that has a char parameter for
plan choice and a double for minutesCharge. Here printResults() is to print
planDescription("Daily" or "Minute") and minutesCharge with side labels.
6. Invoke printResults() passing arguments planChoice and minutesCharge.