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:

    “Perspective, as its inventor remarked, is a beautiful thing. What horrors of damp huts, where human beings languish, may not become picturesque through aerial distance! What hymning of cancerous vices may we not languish over as sublimest art in the safe remoteness of a strange language and artificial phrase! Yet we keep a repugnance to rheumatism and other painful effects when presented in our personal experience.”
    —George Eliot [Mary Ann (or Marian)

    “Surely, ‘tis one step towards acting well, to think worthily of our nature; and as in common life, the way to make a man honest, is, to suppose him so ... so here, to set some value upon ourselves, enables us to support the character ... of generosity and virtue.”
    —Laurence Sterne (1713–1768)