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:

    Which I wish to remark—
    And my language is plain—
    That for ways that are dark
    And for tricks that are vain,
    The heathen Chinee is peculiar:
    Which the same I would rise to explain.
    Bret Harte (1836–1902)

    No one—man or woman—can have it all without support from the workplace and genuine help at home. Women, regardless of how they have chosen to lead their lives, can now breathe a collective sigh of relief that superwoman is dead.
    Deborah J. Swiss (20th century)