Function (computer programming)
In computer programming, a function (also procedure, method, subroutine, routine, or subprogram) is a callable unit[1] of software logic that has a well-defined interface and behavior and can be invoked multiple times.Judicious application can reduce the cost of developing and maintaining software, while increasing its quality and reliability.For example: The idea of a callable unit was initially conceived by John Mauchly and Kathleen Antonelli during their work on ENIAC and recorded in a January 1947 Harvard symposium on "Preparation of Problems for EDVAC-type Machines."[4] Maurice Wilkes, David Wheeler, and Stanley Gill are generally credited with the formal invention of this concept, which they termed a closed sub-routine,[5][6] contrasted with an open subroutine or macro.[7] However, Alan Turing had discussed subroutines in a paper of 1945 on design proposals for the NPL ACE, going so far as to invent the concept of a return address stack.[9][10] In January 1947 John Mauchly presented general notes at 'A Symposium of Large Scale Digital Calculating Machinery' under the joint sponsorship of Harvard University and the Bureau of Ordnance, United States Navy.Memory was a very scarce resource on early computers, and subroutines allowed significant savings in the size of programs.In systems such as the HP 2100, the JSB instruction would perform a similar task, except that the return address was stored in the memory location that was the target of the branch.Each stack frame contains the private data of the corresponding call, which typically includes the procedure's parameters and internal variables, and the return address.[citation needed] Some designs, notably some Forth implementations, used two separate stacks, one mainly for control information (like return addresses and loop counters) and the other for data.[citation needed] With this scheme, the compiler does not have to reserve separate space in memory for the private data (parameters, return address, and local variables) of each procedure.[citation needed] For such programs, the call stack mechanism could save significant amounts of memory.Indeed, the call stack mechanism can be viewed as the earliest and simplest method for automatic memory management.[17] An environment that fully supports coroutines or lazy evaluation may use data structures other than stacks to store their activation records.Most modern programming languages provide features to define and call functions, including syntax for accessing such features, including: Some languages, such as Pascal, Fortran, Ada and many dialects of BASIC, use a different name for a callable unit that returns a value (function or subprogram) vs. one that does not (subroutine or procedure).Most contexts support local variables – memory owned by a callable to hold intermediate values.If the programmer desires the recursive callable to use the same variables instead of using locals, they typically declare them in a shared context such static or global.[22] Early computer instruction sets made storing return addresses and variables on a stack difficult.In the IBM CICS transaction processing system, quasi-reentrant was a slightly less restrictive, but similar, requirement for application programs that were shared by many threads.Some languages support overloading – allow multiple callables with the same name in the same scope, but operating on different types of input.Besides its happy path behavior, a callable may need to inform the caller about an exceptional condition that occurred during its execution.In the IBM System/360, where return code was expected from a subroutine, the return value was often designed to be a multiple of 4—so that it could be used as a direct branch table index into a branch table often located immediately after the call instruction to avoid extra conditional tests, further improving efficiency.Inlining, however, usually increases the compiled code size, except when only called once or the body is very short, like one line.They argue that callables are key components in software maintenance, and their roles in the program must remain distinct.This code repeatedly asks the user to enter a number and reports the square root of the value.In Microsoft Small Basic, targeted to the student first learning how to program in a text-based language, a callable unit is called a subroutine.[26] In later versions of Visual Basic (VB), including the latest product line and VB6, the term procedure is used for the callable unit concept.[28] VB supports parameter passing conventions by value and by reference via the keywords ByVal and ByRef, respectively.In PL/I a called procedure may be passed a descriptor providing information about the argument, such as string lengths and array bounds.A rule (or clause) of the form: which has the logical reading: behaves as a procedure that reduces goals that unify with A to subgoals that are instances ofB.