Programming Fundamentals/Practice: Functions
- Modular Programming - A software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.
- Hierarchy or Structure Chart - Conveys the relationship or big picture of the various functions in a program.
- Function Examples
- Parameters and Arguments - A parameter is a special kind of variable used in a function to refer to one of the pieces of data provided as input to the function. These pieces of data are the values of the arguments with which the function is going to be called/invoked.
- Call by Value vs Call by Reference - Parameters can be passed by two different methods depending on the language.
- Call by Value passes parameters as copies of the original value creating a second location in memory, which is essentially a new local variable. Any changes made to a Call by Value parameter only affect the passed in value and not the value passed in by the caller. This is the most common method that is used by most modern programming languages.
- Call by Reference passes in the parameter using a single memory location which essentially passes in the original parameter from the calling function. Any changes made to a Call by Reference parameter by the called function also changes the value of that parameter in the calling function.
- Return Statement - Causes the result of a function to leave the current function and resume at the point in the code immediately after where the function was called.
- Void Data Type - A data type that has no values or operators and is used to represent nothing.
- Scope - The area of a source code file where an identifier name is recognized.
- Programming Style - A set of rules or guidelines used when writing the source code for a computer program.
- Standard Libraries - Common functions whose definitions have already been written that are ready to be used in any program.
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.
- Functions must always have a return value.
- 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.
- 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 have confidence that standard library functions work properly.
- It would be easier to write programs without using specific task functions.
- In order to separate the functionality of one function from another, you need to give each function its own parameters.
- Call-by-value evaluates arguments and passes a copy of the value to the subroutine.
- All functions should be called from "Output" because "Output" needs to call information from the other functions to get the correct output.
- Functions can only return 1 output.
- false - Making source code readable should be a priority for all programmers.
- false – It may seem difficult at first, but with a little practice it is really quite easy.
- false - That may be the case in very simple code, but when the programs becomes complicated it becomes crucial to use functions.
- false - All functions should be called from main, because main is in charge of the other functions.
- true - only Python has the ability to return multiple variables from a single function.
- 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.
- Describe the differences between the local scope and the global scope.
- 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?
- In your own words describe the difference between calling by value and calling by reference.
- Explain the differences between the void data type and the Nothing data type.
- Explain why having multiple functions in a program is more useful than having everything in the Main() function.
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 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.
- 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 * heightCalculate the number of gallons as:
total area / square feet per gallonNote: You must round up to the next full gallon. To round up, add
0.9999and then convert the resulting value to an integer. Calculate the total cost of the paint as:
gallons * price per gallon.