Programming Fundamentals/Practice: Functions

From Wikibooks, open books for an open world
< Programming Fundamentals
Jump to navigation Jump to search

Chapter Summary[edit]

  • Modular Programming
  • Hierarchy or Structure Chart
  • Function Examples
  • Parameters and Arguments
  • Call by Value vs Call by Reference
  • Return Statement
  • Void Data Type
  • Scope
  • Programming Style
  • Standard Libraries

Review Questions[edit]

True / False[edit]

  1. In addition to the term function as the name of a subprogram, the computer industry also uses macro, procedure and module.
  2. Generally, functions fall into two categories: Program Control and Specific Task.
  3. Hierarchy Charts and Structure Charts are basically the same thing.
  4. Program Control functions are used to simply subdivide and control the program.
  5. The void data type is rarely used in C++.
  6. Making source code readable is only used by beginning programmers.
  7. Scope refers to a brand of mouthwash.
  8. User-defined specific task functions are usually placed into a user-defined library.
  9. Local and global data storage is associated with the concept of scope.
  10. Creating a header file for user-defined specific task functions is a difficult task.
  11. The stack is part of the computer’s memory used for storage of data.
  12. The standard library is a set of specific task functions that have been added to the programming language for universal use.
  13. Programmers should not have confidence that standard library functions work properly.
  14. It would be easier to write programs without using specific task functions.
  15. In order to separate the functionality of one function from another, you need to give each function its own parameters.
  16. Call-by-value evaluates arguments and passes a copy of the value to the subroutine.
  17. All functions should be called from "Output" because "Output" needs to call information from the other functions to get the correct Output.

Answers:

  1. true
  2. true
  3. true
  4. true
  5. false
  6. false - While this may see, true, having a code that everybody can read is stilled used my professionals.
  7. false – Although Scope is a brand of mouthwash; we are looking for the computer-related definition.
  8. true
  9. true
  10. false – It may seem difficult at first, but with a little practice it is really quite easy.
  11. true
  12. true
  13. false- While there may be slight differences each program has very similar library functions.
  14. false- That may be the case in very simple code, but when the programs becomes complicated is begins to be come crucial to use functions.
  15. true - To separate functions, each function is given its own unique input variables called parameters.
  16. true
  17. false - All functions should be called from main, because main is in charge of the other functions.

Short Answer[edit]

  1. Create a hierarchy chart for the function example program found in this chapter.
  2. 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.
  3. Describe the differences between the local scope and the global scope.
  4. Think about when you are creating a function that requires more than one parameter to be passed in. Then when calling that function, in what order do you place those parameters and why?
  5. In your own words describe the difference between calling by value and calling by reference.

Activities[edit]

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.

  1. Create a program to prompt the user for hours and rate per hour and then compute gross pay (hours * rate).[1], or create a program that asks the user for their monthly income and then calculate and display their annual income (monthly income * 12).
  2. 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.
  3. 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.
  4. 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.
  5. 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 to a yard.
  6. 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, add 0.9999 and then convert the resulting value to an integer. Calculate the total cost of the paint as: gallons * price per gallon.

References[edit]