Practice: Functions
Review Questions
True / False
- In addition to the term function as the name of a subprogram, the computer industry also uses macro, procedure and module.
- Generally, functions fall into two categories: Program Control and Specific Task.
- Hierarchy Charts and Structure Charts are basically the same thing.
- Program Control functions are used to simply subdivide and control the program.
- The void data type is rarely used in C++.
- Making source code readable is only used by beginning programmers.
- Scope refers to a brand of mouthwash.
- User-defined specific task functions are usually placed into a user-defined library.
- Local and global data storage is associated with the concept of scope.
- Creating a header file for user-defined specific task functions is a difficult task.
- The stack is part of the computer’s memory used for storage of data.
- The standard library is a set of specific task functions that have been added to the programming language for universal use.
- Programmers should not have confidence that standard library functions work properly.
- It would be easier to write programs without using specific task functions.
Answers:
- true
- true
- true
- true
- false
- false
- false – Although Scope is a brand of mouthwash; we are looking for the computer-related definition.
- true
- true
- false – It may seem difficult at first, but with a little practice it is really quite easy.
- true
- true
- false
- false
Short Answer
- Create a hierarchy chart for the function example program found in this chapter.
- Review the programs you have already created for this course. Based on coding standards for your selected programming language, identify some problems that make your code “undocumented”, “unreadable” or wrong in some other way.
Activities
Complete the following activities using pseudocode, a flowcharting tool, or your selected programming language. Use separate functions for input, each type of processing, and output. Avoid global variables by passing parameters and returning results. Create test data to validate the accuracy of each program. Add comments at the top of the program and include references to any resources used.
- Create a program to prompt the user for hours worked per week and rate per hour and then calculate and display their weekly, monthly, and annual gross pay (hours * rate). Base monthly and annual calculations on 12 months per year and 52 weeks per year.[1]
- Create a program that asks the user how old they are in years, and then calculate and display their approximate age in months, days, hours, and seconds. For example, a person 1 year old is 12 months old, 365 days old, etc.
- Review MathsIsFun: US Standard Lengths. Create a program that asks the user for a distance in miles, and then calculate and display the distance in yards, feet, and inches, or ask the user for a distance in miles, and then calculate and display the distance in kilometers, meters, and centimeters.
- Review MathsIsFun: Area of Plane Shapes. Create a program that asks the user for the dimensions of different shapes and then calculate and display the area of the shapes. Do not include shape choices. That will come later. For now, just include multiple shape calculations in sequence.
- Create a program that calculates the area of a room to determine the amount of floor covering required. The room is rectangular with the dimensions measured in feet with decimal fractions. The output needs to be in square yards. There are 3 linear feet (9 square feet) to a yard.
- Create a program that helps the user determine how much paint is required to paint a room and how much it will cost. Ask the user for the length, width, and height of a room, the price of a gallon of paint, and the number of square feet that a gallon of paint will cover. Calculate the total area of the four walls as
2 * length * height + 2 * width * height
Calculate the number of gallons as:total area / square feet per gallon
Note: You must round up to the next full gallon. To round up, add0.9999
and then convert the resulting value to an integer. Calculate the total cost of the paint as:gallons * price per gallon
. - Review Wikipedia: Aging in dogs. Create a program to prompt the user for the name of their dog and its age in human years. Calculate and display the age of their dog in dog years, based on the popular myth that one human year equals seven dog years. Be sure to include the dog’s name in the output, such as:
Spike is 14 years old in dog years.
References
- archive.org: Programming Fundamentals – A Modular Structured Approach using C++
- Wikiversity: Computer Programming