Microsoft Store
 

Subroutine


 

In computer science, a subroutine (function, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one or more statement blocks; such code is sometimes collected into software libraries. Subroutines can be "called", thus allowing programs to access the subroutine repeatedly without the subroutine's code having been written more than once. In many programming languages the word function is reserved for subroutines that return a value; however, the C programming language and its programmers view subroutines simply as functions that do not return a value.

Advantages of subprograms

There are numerous motivations for the use of subprograms:

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

  • to reduce redundancy in a program,
  • to enable reuse of code across multiple programs,
  • to decompose complex problems into simpler pieces,
  • to improve readability of a program,
  • to replicate useful mathematical functions,
  • to hide or regulate part of the program (see Information hiding),
  • to improve maintainability and reduce risk of errors,
  • to improve ease of extension.
  • Generally, to make use of a subprogram, a programmer places some form of call instruction--which constitutes a call site--into an instruction sequence. When the call site is encountered, the instruction sequence is temporarily suspended, and the subprogram itself executes until it completes, at which time the original instruction sequence resumes.

    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~