Subroutine

From StudyWiki

(Redirected from Procedure)
Jump to: navigation, search


Overview

  • A subroutine (function, method, procedure, sub procedure, or subprogram) is a portion of code within a larger program, which performs a specific task and can be relatively independent of the remaining code.
  • Subroutines are similar to functions but differ in that they do not have to return a value.
    • A function may be viewed as a special instance of a subroutine
  • Some programming languages make very little syntactic distinction between functions and subroutines.


Components

The components of a subroutine may include:

  • a body of code to be executed when the subroutine is called,
  • parameters that are passed to the subroutine from the point where it is called, and
  • a value that is returned to the point where the call occurs.


Advantages

  • reducing the duplication of code in a program.
  • enabling reuse of code across multiple programs,
  • decomposing complex problems into simpler pieces (this improves maintainability and ease of extension),
  • improving readability of a program, and
  • hiding or regulating part of the program.