|
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. Wikibooks Wikiversity has more about this subject: School of Computer Science Open Directory Project: Computer Science Downloadable Science and Computer Science books Collection of Computer Science Bibliographies Belief that title science in computer science is inappropriate nacho Categories: Computer science ...
Source code (commonly just source or code) is any series of statements written in some human-readable computer programming language. ...
In common language, a task is part of a set of actions which accomplish a job; the sense is that useful work is getting done. Task analysis is the analysis or a breakdown of exactly how a task is accomplished, such as what sub-tasks are required. ...
// A computer program or software program (usually abbreviated to a program) is a step-by-step list of instructions written for a particular computer architecture in a particular computer programming language. ...
In computer programming, a statement block is a section of code which is grouped together, much like a paragraph; such blocks consist of one, or more, statements. ...
Illustration of an application which may use libvorbisfile. ...
Call option Computer-assisted language learning Categories: Acronyms ...
Source code (commonly just source or code) is any series of statements written in some human-readable computer programming language. ...
Other listings of programming languages are: Categorical list of programming languages Generational list of programming languages Chronological list of programming languages Note: Esoteric programming languages have been moved to the separate List of esoteric programming languages. ...
The C Programming Language, Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language The C programming language is a low-level standardized programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the...
History
The first use of subprograms was in assembly languages that did not have a call instruction. On these computers, subroutines needed to be called by a sequence of lower level instructions, possibly implemented as a macro. These instructions typically modified the program code rather like the infamous Cobol alter statement, modifying the address of a branch at a standard location so that it behaved like an explicit return instruction. Even with this cumbersome approach subroutines proved so useful that soon most architectures provided instructions to help with subroutine calls, clear up to explicit call instructions. Assembly language or simply assembly is a human-readable notation for the machine language that a specific computer architecture uses. ...
COBOL is a third-generation programming language. ...
The term statement can have several meanings: In programming, a statement is an instruction to execute something that will not return a value. ...
When an assembly language program executes a call, program flow jumps to another location, but the address of the next instruction (that is, the instruction that follows the call instruction in memory) is kept somewhere to use when returning. The IBM 360 range used to save this address in a processor register, relying on macros to save and restore deeper return addresses in memory associated with individual subroutines, then using branches to the address specified in the register to accomplish a subroutine return. However stacks have proved to be a better approach, since they automatically cater for recursive subroutine calls, and were typically used in all but a few later architectures. In a stack based architecture, the return address is 'pushed' as a point of return on the stack. The subroutine exits by 'pop'ing a return value from the top of the stack, which reads the previously pushed return address and jumps to it, so that program flow continues immediately after the call instruction. Most RISC and VLIW architectures save the return address in a link register (as the IBM 360 did), but simulate a stack with load and store instructions rather than with push and pop instructions. In computer architecture, a processor register is a small amount of very fast computer memory used to speed the execution of computer programs by providing quick access to commonly used values—typically, the values being in the midst of a calculation at a given point in time. ...
In computer programming, subprograms (functions) will return to the higher-level programs, which called them; return ends the current task. ...
It has been suggested that Stack-Based Memory Allocation be merged into this article or section. ...
In mathematics and computer science, recursion is a particular way of specifying (or constructing) a class of objects (or an object from a certain class) with the help of a reference to other objects of the class: a recursive definition defines objects in terms of the already defined objects of...
A link register, in the IBM POWER CPU architecture, is a special purpose register which holds the address to return to when a function call completes. ...
Technical overview A subprogram, as its name suggests, somehow behaves like a computer program. Typically, the caller waits for subprograms to finish and continues execution only after a subprogram "returns". Subroutines are often given parameters to refine their behavior or to perform a certain computation with given [variable] values. Generally, subprograms execute their statements from top to bottom. In computer programming, subprograms (functions) will return to the higher-level programs, which called them; return ends the current task. ...
A parameter is a measurement or value on which something else depends. ...
In most imperative programming languages, subprograms may have so-called side-effects; that is, they may cause changes that remain after the subprogram has returned. Usually, compilers cannot predict whether a subprogram has a side-effect or not, but can determine if a subprogram calls no other subprograms, or at least no other subprograms that have side-effects. In imperative programming, compilers usually assume every subprogram has a side-effect to avoid complex analysis of execution paths. Because of its side-effects, a subprogram may return different results each time it is called, even if it is called with the same arguments. A simple example is a subprogram that implements a Pseudorandom number generator; that is, a subprogram that returns a random number each time it is called. In computer science, imperative programming, as opposed to declarative programming, is a programming paradigm that describes computation in terms of a program state and statements that change the program state. ...
In computer science, a side-effect is a property of a programming language function that it modifies some state other than its return value. ...
A pseudorandom number generator (PRNG) is an algorithm that generates a sequence of numbers, the elements of which are approximately independent of each other. ...
Such behavior is invalid in a strict mathematical sense. An exception to this common behaviour is found in functional programming languages, where subprograms can have no side effects, and will always return the same result if repeatedly called with the same arguments. (Note that subprograms are referred to as functions in these languages.) The Haskell programming language logo Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions. ...
Low-level implementation This section deals with the modern implementation of having subroutine data stored on one or more stacks. Due to usage of a stack, a subroutine can call itself (see recursion) or other subroutines (nested calls), and of course it can call the same subroutine from several distinct places. Assembly languages generally do not provide programmers with such conveniences as local variables or subroutine parameters. They get to be implemented by passing values in registers or pushing them onto the stack (or another stack, if there is more than one). In mathematics and computer science, recursion is a particular way of specifying (or constructing) a class of objects (or an object from a certain class) with the help of a reference to other objects of the class: a recursive definition defines objects in terms of the already defined objects of...
When there is just one stack, the return addresses must be placed in the same space as the parameters and local variables. Hence, a typical stack may look like this (for a case where function1 calls function2): This article needs to be cleaned up to conform to a higher standard of quality. ...
In computer science, a local variable is a variable that is given local scope. ...
- previous stack data
- function1 local variables
- parameters for function2
- function1 return address (of the instruction which called function2)
- function2 local variables.
This is with a forwards-growing stack — on many architectures the stack grows backwards in memory. Forward and backwards-growing stacks are useful because it is quite practical to have two stacks growing towards each other in a common scratch space, using one mainly for control information like return addresses and loop counters and the other for data. (This is what Forth does.) Forth is a procedural, data-structured, reflective, programming language and programming environment. ...
The parts of the program which are responsible for the entry into and exit out of the subroutine (and hence, the setting up and removal of each stack frame) are called the function prologue and epilogue. In assembly language programming, the function prologue is a few lines of code which appear at the beginning of a function, which prepare the stack and registers for use within the function. ...
If the procedure or function itself uses stack handling commands, outside of the prologue and epilogue, e.g. to store intermediate calculation values, the programmer needs to keep track of the number of 'push' and 'pop' instructions so as not to corrupt the original return address.
C and C++ examples In the C and C++ programming languages, subprograms are referred to as "functions". Note that these languages use the special keyword void to indicate that a function does not return any value — that is it only has side-effects. The C Programming Language, Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language The C programming language is a low-level standardized programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the...
C++ (pronounced see plus plus, IPA: /siË plÉs plÉs/) is a general-purpose computer programming language. ...
Below are three such functions - the first function does absolutely nothing; it is called with: "function1();. The second function returns the number 5; the function can be called with: "function2();" The third function returns a desired selection (1-5), and is called with: "function3(number);". void function1(void) { } int function2(void) { return 5; } int function3(int number) { int selection[] = {5,1,3,2,4}; return selection[number]; } 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. Redundancy, in general terms, refers to the quality or state of being redundant, that is: exceeding what is necessary or normal, containing an excess. ...
In mathematics, a function is a relation, such that each element of a set (the domain) is associated with a unique element of another (possibly the same) set (the codomain, not to be confused with the range). ...
// Overview In computer science, the principle of information hiding is the hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed. ...
Local variables, recursion and re-entrancy A subprogram may find it useful to make use of a certain amount of "scratch" space; that is, memory used during the execution of that subprogram to hold intermediate results. Variables stored in this scratch space are referred to as local variables, and the scratch space itself is referred to as an activation record. An activation record typically has a return address that tells it where to pass control back to when the subprogram finishes. In both conventional and electronic messaging, a return address is an explicit inclusion of the address of the person sending the message. ...
A subprogram may have any number and nature of call sites; in fact, a subprogram may even call itself, causing its execution to suspend while another nested execution of the same subprogram occurs. This is referred to as recursion, and is a useful technique for making some complex algorithms more comprehensible. However, recursion poses a problem if the recursive execution modifies any local variables, because when the suspended execution resumes, it will find that the data stored in its local variables have been lost. In mathematics and computer science, recursion is a particular way of specifying (or constructing) a class of objects (or an object from a certain class) with the help of a reference to other objects of the class: a recursive definition defines objects in terms of the already defined objects of...
Early languages like Fortran simply didn't support recursion for this reason. Modern languages almost invariably provide a fresh activation record for every execution of a subprogram; that way, the nested execution is free to modify its local variables without concern for the effect on other suspended executions in progress. As nested calls accumulate, a call stack structure is formed, consisting of one activation record for each suspended subprogram. In fact, this stack structure is virtually ubiquitous, and so activation records are commonly referred to as stack frames. Fortran (also FORTRAN) is a statically typed, compiled imperative computer programming language originally developed in the 1950s and still heavily used for scientific computing and numerical computation half a century later. ...
It has been suggested that Stack-Based Memory Allocation be merged into this article or section. ...
In computing, a stack frame is a data structure used to create temporary storage for data and saved state in functions. ...
If a subprogram can function properly even when called while another execution is already in progress, that subprogram is said to be re-entrant. A recursive subprogram must be re-entrant. Re-entrant subprograms are also useful in multi-threaded situations, since multiple threads can call the same subprogram without fear of interfering with each other. A thread in computer science is short for a thread of execution or a sequence of instructions. ...
In a multi-threaded environment, there is generally more than one stack. An environment which fully supports coroutines or lazy evaluation may use data structures other than stacks to store their activation records. A thread in computer science is short for a thread of execution or a sequence of instructions. ...
Coroutines are program components like subroutines. ...
In computer programming, lazy evaluation is a concept that attempts to minimize the work the computer has to do. ...
Conventions A number of conventions for the coding of subprograms have been developed. It has been commonly preferable that the name of a subprogram should be a verb when it does a certain task, an adjective when it makes some inquiry, and a noun when it is used to substitute variables and such. Experienced programmers recommend that a subprogram perform only one task. If a subprogram performs more than one task, it should be split up into more subprograms. They argue that subprograms are key components in maintaining code and their roles in the program must be distinct. Some advocate that each subprogram should have minimal dependency on other pieces of code. For example, they see the use of global variables as unwise because it adds tight-coupling between subprograms and global variables. If such coupling is not necessary at all, they advise to refactor subprograms to take parameters instead. This practice is controversial because it tends to increase the number of passed parameters to subprograms. See programming practice for a more detailed discussion of programming disciplines. This is the list of programming practices: Adapt to adapt Law of Demeter Assertion Once and only once See also: Design pattern Contrast with: Bad programming practices Categories: Substubs ...
Related terms and clarification Different programming languages and methodologies possess notions and mechanisms related to subprograms: - Subroutine is practically synonymous with "subprogram." The former term may derive from the terminology of assembly languages and Fortran.
- Function and procedure often denote a subprogram that takes parameters and may or may not have a return value. Many make the distinction between "functions", that possess return values and appear in expressions, versus "procedures", that possess no return values and appear in statements [though this is not a distinction found in the C programming language]. (See also Command-Query Separation.)
- Method is a special kind of subprogram used in object-oriented programming that describes some behaviour of an object.
- Closure is a subprogram together with the values of some of its variables captured from the environment in which it was created.
- Coroutine is a subprogram that returns to its caller before completing.
- Event handler, or simply "handler," is a subprogram that is called in response to an "event", such as a computer user moving the mouse or typing on the keyboard. The AppleScript scripting language simply uses the term "handler" as a synonym for subprogram.
- Threaded code makes code even more compact. It uses a small interpreter to execute subroutines that consist of lists of subroutine addresses. The lowest levels of subroutine are the only machine language.
Assembly language or simply assembly is a human-readable notation for the machine language that a specific computer architecture uses. ...
Fortran (also FORTRAN) is a statically typed, compiled, programming language originally developed in the 1950s and still heavily used for scientific computing and numerical computation half a century later. ...
An expression in a programming language is a combination of values and functions or procedures, interpreted according to the particular rules of precedence and of association for a particular programming language, which computes and then returns another value. ...
A statement is the minimal unit of structuring in imperative programming languages. ...
Command-query separation (CQS) is a principle of object-oriented computer programming. ...
Used mainly in object-oriented programming, the term method refers to a piece of code that is exclusively associated either with a class (called class methods or static methods) or with an object (called instance methods). ...
In computer science, object-oriented programming, OOP for short, is a computer programming paradigm. ...
In strictly mathematical branches of computer science the term object is used in a purely mathematical sense to refer to any thing. While this interpretation is useful in the discussion of abstract theory, it is not concrete enough to serve as a primitive in the discussion of more concrete branches...
In programming languages, a closure is an abstraction representing a function, plus the lexical environment (see static scoping) in which the function was created. ...
Coroutines are program components like subroutines. ...
An event handler is a part of a computer program created to tell the program how to act in response to a specific event (e. ...
AppleScript is a scripting language devised by Apple Computer, and built into Mac OS. More generally, AppleScript is the word used to designate the Mac OS scripting interface, which is meant to operate in parallel with the graphical user interface. ...
In computer science, the term threaded code refers to an implementation technique for programming languages that produces very compact code. ...
An interpreter is a computer program that executes other programs. ...
See also |