This lesson introduces functions. In different programming languages, a function may be called a subroutine, a procedure, a routine, a method, or a subprogram. The generic term callable unit is sometimes used.
Objectives and Skills
Objectives and skills for this lesson include:
- Understand the benefits of modular programming
- Understand subroutines, passed parameters, and return values
- Understand variable scope
- Use subroutines/functions/methods to implement program functionality
- Use local variables, passed parameters, and return values
- Rebus: Programming Fundamentals
- Wikipedia: Modular programming
- Wikipedia: Subroutine
- Wikipedia: Parameter (computer programming)
- Wikipedia: Scope (computer science)
- Wikipedia: Naming convention (programming)
- YouTube: Understanding Structure
- YouTube: The advantages of modularization
- YouTube: Modularizing a program
- YouTube: Programming For Beginners - Functions
- YouTube: Introduction to Programming - Functions
- YouTube: Programming Basics #21 Functions
- YouTube: Programming Basics #22 Parameters and Arguments
- YouTube: Programming Basics #23 Variable Scope
- YouTube: Naming Convention With Programming Languages
Complete the following activities using a flowchart tool, pseudocode, or your selected programming language. Use separate functions for input, processing, and output. Avoid global variables by passing parameters and returning results.
- Create a program to prompt the user for hours and rate per hour and then compute gross pay (hours * rate).
- 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.
- 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.
- Review Wikipedia: Zeller's congruence. Create a program that asks the user for their birthday (year, month, and day) and then calculate and display the day of the week on which they were born.
- Modular programming is 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.
- Arguments are passed to the subroutine by direct reference, typically using the argument's address.
- Arguments are evaluated and a copy of the value is passed to the subroutine.
- return value
- A sequence of program instructions that perform a specific task, packaged as a unit.
- Procedures and functions
- Introduction to Programming/Subprograms
- Introduction to Programming/Scope
- BBC: Functions, procedures and modules