Assessment Tool

Lecture 7:  Functions

Content Tested: When to Use Functions

Lecture Content:

Goals:

Assessment Technique:  Problem Recognition Tasks

Purpose:

This activity allows instructors to see if students understand where using functions is appropriate.

Activity:

In today's lecture, we learned why functions are useful in programming.  Examine the formatting of the cost reports shown below.  (Ignore the funny names -- they are taken from the Harry Potter series.)  List any functions that might be useful in generating these reports (don't worry about the computational aspect of calculating the numbers; focus on functions that aid in the printing/formatting of the information).  Please list the function name that would be appropriate, a description of what the function does, and reasons why you think this is a helpful function.  Don't worry about writing any C code here.  We'll learn more about functions in the next lecture.

==============================================================================
How many ounces of wormwood are needed? 0
How many ounces of nettles are needed? 5.0
How many snake fangs are needed? 20
How many horned slugs are needed? 0
How many porcupine quills are needed? 20
How many potion vials are needed? 10

*-----*-----*-----*-----*-----*
MalWart:
        Item        Quantity
*-----*-----*-----*-----*-----*
Snake Fangs          20.00
Porcupine Quills     20.00
Potion Vials         10.00
*-----*-----*-----*-----*-----*
 

Hit any key, then enter to continue
x

*-----*-----*-----*-----*-----*
California Dreams:
        Item        Quantity
*-----*-----*-----*-----*-----*
Nettles (in oz.)      5.00
*-----*-----*-----*-----*-----*
 

Hit any key, then enter to continue
x

*-----*-----*-----*-----*-----*
Local price list:
        Item        Ga Si Kn
*-----*-----*-----*-----*-----*
Snake Fangs          0 10 28
Porcupine Quills     0  2 11
Potion Vials         0 10  9
Nettles (in oz.)     0  9 16
*-----*-----*-----*-----*-----*
 

Hit any key, then enter to continue
x
==============================================================================

To get you started, here is an example function.

Function Name:  PrintSeparator

Description:  This function prints the line
*-----*-----*-----*-----*-----*
to use as a separator line in the reports.

Helpfulness:  If a person wanted to change the separator to be all asterisks, this could be done easily if the line is printed using a function.
 

Possible Solutions

Function:  PrintItemQuantity

Description:  This function prints one price report listing the items and quantities at certain stores.  This will also call the function PrintSeparator.

Helpfulness:  If several reports need to be printed (as in this case), it makes sense to create one function to print each item and quantity summary.  Also, if the company wanted to change the format of the item and quantity summaries this could be done easily with the modification of a single function.

Function:  PrintHitAnyKey

Description:  This function prints "Hit any key, then enter to continue".

Helpfulness:  This function is used multiple times in the printing of the report and allows the programmer to specify once the printf statement containing "Hit any key, then enter to continue".

Function:  PrintLocalPrices

Description:  This function prints the local price list with the items and the costs.

Helpfulness:  Putting the printing of the price list in a single function keeps the  code organized and allows for an easy change if the format is to be modified.

Possible Uses of Activity: