Computer Programming/Subroutines

From Wikiversity
Jump to: navigation, search
Flowchart calling 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[edit]

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

Readings[edit]

  1. Wikipedia: Modular programming
  2. Wikipedia: Subroutine
  3. Wikipedia: Parameter (computer programming)
  4. Wikipedia: Scope (computer science)
  5. Wikipedia: Naming convention (programming)

Multimedia[edit]

  1. YouTube: Understanding Structure
  2. YouTube: The advantages of modularization
  3. YouTube: Modularizing a program
  4. YouTube: Programming For Beginners - Functions
  5. YouTube: Introduction to Programming - Functions
  6. YouTube: Programming Basics #21 Functions
  7. YouTube: Programming Basics #22 Parameters and Arguments
  8. YouTube: Programming Basics #23 Variable Scope
  9. YouTube: Naming Convention With Programming Languages

Examples[edit]

Activities[edit]

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[edit]

  • 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[edit]

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]

Assessments[edit]

See Also[edit]

References[edit]