Subroutine - Language Support

Language Support

High-level programming languages usually include specific constructs to:

  • delimit the part of the program (body) that makes up the subroutine
  • assign an identifier (name) to the subroutine
  • specify the names and/or data types of its parameters and/or return values
  • provide a private naming scope for its temporary variables
  • identify variables outside the subroutine that are accessible within it
  • call the subroutine
  • provide values to its parameters
  • specify the return values from within its body
  • return to the calling program
  • dispose of the values returned by a call
  • handle any exceptional conditions encountered during the call
  • package subroutines into a module, library, object, class, etc.

Some programming languages, such as Visual Basic .NET, Pascal, Fortran, and Ada, distinguish between functions or function subprograms, which provide an explicit return value to the calling program, and subroutines or procedures, which do not. In those languages, function calls are normally embedded in expressions (e.g., a sqrt function may be called as y = z + sqrt(x)); whereas procedure calls behave syntactically as statements (e.g., a print procedure may be called as if x > 0 then print(x). Other languages, such as C and Lisp, do not make this distinction, and treat those terms as synonymous.

In strictly functional programming languages such as Haskell, subprograms can have no side effects, and will always return the same result if repeatedly called with the same arguments. Such languages typically only support functions, since subroutines that do not return a value have no use unless they can cause a side effect.

In programming languages such as C, C++, and C#, subroutines may also simply be called functions, not to be confused with mathematical functions or functional programming, which are different concepts.

A language's compiler will usually translate procedure calls and returns into machine instructions according to a well-defined calling convention, so that subroutines can be compiled separately from the programs that call them. The instruction sequences corresponding to call and return statements are called the procedure's prologue and epilogue.

Read more about this topic:  Subroutine

Famous quotes containing the words language and/or support:

    Jargon: any technical language we do not understand.
    Mason Cooley (b. 1927)

    A clergyman, again, can hardly ever allow himself to look facts fairly in the face. It is his profession to support one side; it is impossible, therefore, for him to make an unbiased examination of the other.
    Samuel Butler (1835–1902)