# Computer Programming/Subroutines

This lesson introduces subroutines. In different programming languages, a subroutine may be called a procedure, a function, a routine, a method, or a subprogram. The generic term callable unit is sometimes used.[1]

## 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

## Activities

Complete the following activities using a flowchart tool, pseudocode, or your selected programming language.

1. Create a program to prompt the user for hours and rate per hour and then compute gross pay (hours * rate).[2] Use separate subroutines/functions/methods for input, processing, and output. Avoid global variables by passing parameters and returning results.
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. Use separate subroutines/functions/methods for input, processing, and output. Avoid global variables by passing parameters and returning results.
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. Use separate subroutines/functions/methods for input, processing, and output. Avoid global variables by passing parameters and returning results.
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. Use separate subroutines/functions/methods for input, processing, and output. Avoid global variables by passing parameters and returning results.
5. 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. Use separate subroutines/functions/methods for input, processing, and output. Avoid global variables by passing parameters and returning results.

## Lesson Summary

• 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.[3]

## Key Terms

argument
call-by-reference
Arguments are passed to the subroutine by direct reference, typically using the argument's address.[4]
call-by-value
Arguments are evaluated and a copy of the value is passed to the subroutine.[5]
parameter
return value
scope
subroutine
A sequence of program instructions that perform a specific task, packaged as a unit.[6]