A wrapper function is a
function (another word for a ''subroutine'') in a
software library or a
computer program
A computer program is a sequence or set of instructions in a programming language for a computer to Execution (computing), execute. It is one component of software, which also includes software documentation, documentation and other intangibl ...
whose main purpose is to call a second subroutine or a
system call
In computing, a system call (syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, accessing a hard disk drive ...
with little or no additional computation. Wrapper functions simplify writing computer programs by abstracting the details of a subroutine's implementation.
Purpose
Wrapper functions are a means of
delegation and can be used for a number of purposes.
Programming convenience
Wrapper functions simplify writing computer programs. For example, the
MouseAdapter
and similar classes in the
Java AWT library demonstrate this.
They are useful in the development of applications that use third-party library functions. A wrapper can be written for each of the third party functions and used in the native application. In case the third party functions change or are updated, only the wrappers in the native application need to be modified as opposed to changing all instances of third party functions in the native application.
Adapting class/object interfaces
Wrapper functions can be used to
adapt an existing class or object to have a different interface. This is especially useful when using existing library code.
Code testing
Wrapper functions can be used to write
error checking
In information theory and coding theory with applications in computer science and telecommunications, error detection and correction (EDAC) or error control are techniques that enable reliable delivery of digital data over unreliable communic ...
routines for pre-existing system functions without increasing the length of a code by a large amount by repeating the same error check for each call to the function. All calls to the original function can be replaced with calls to the wrapper, allowing the programmer to forget about error checking once the wrapper is written.
A test driver is a kind of wrapper function that exercises a code module, typically calling it repeatedly, with different settings or parameters, in order to rigorously pursue each possible path. It is not deliverable code, but it is not throwaway code either, being typically retained for use in
regression testing
Regression testing (rarely, ''non-regression testing'') is re-running functional and non-functional tests to ensure that previously developed and tested software still performs as expected after a change. If not, that would be called a '' regr ...
.
An interface adaptor is a kind of wrapper function that simplifies, tailors, or amplifies the interface to a code module, with the intent of making it more intelligible or relevant to the user. It may rename parameters, combine parameters, set defaults for parameters, and the like.
Multiple inheritance
In a
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
that does not support
multiple inheritance of base classes, wrapper functions can be used to simulate it. Below is an example of part of a
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
class that "inherits" from LinkedList and HashSet. See
method for further implementation details.
public class Test implements LinkedList, HashSet
Library functions and system calls
Many
library
A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
functions, such as those in the
C Standard Library
The C standard library, sometimes referred to as libc, is the standard library for the C (programming language), C programming language, as specified in the ISO C standard.International Organization for Standardization, ISO/International Electrote ...
, act as
interfaces for
abstraction of
system call
In computing, a system call (syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, accessing a hard disk drive ...
s. The ''fork'' and ''execve'' functions in
glibc
The GNU C Library, commonly known as glibc, is the GNU Project implementation of the C standard library. It provides a wrapper around the system calls of the Linux kernel and other kernels for application use. Despite its name, it now also dir ...
are examples of this. They call the
lower-level ''
fork'' and ''
execve'' system calls, respectively.
This may lead to incorrectly using the terms "system call" and "syscall" to refer to higher-level library calls rather than the similarly named system calls, which they wrap.
Helper function
A helper function is a function which groups parts of
computation
A computation is any type of arithmetic or non-arithmetic calculation that is well-defined. Common examples of computation are mathematical equation solving and the execution of computer algorithms.
Mechanical or electronic devices (or, hist ...
by assigning descriptive names and allowing for the reuse of the computations. Although not all wrappers are helper functions, all helper functions are wrappers, and a notable use of helper functions—grouping frequently utilized operations—is in
dynamic binary translation, in which helper functions of a particular
architecture
Architecture is the art and technique of designing and building, as distinguished from the skills associated with construction. It is both the process and the product of sketching, conceiving, planning, designing, and construction, constructi ...
are used in translation of
instructions from one
instruction set
In computer science, an instruction set architecture (ISA) is an abstract model that generally defines how software controls the CPU in a computer or a family of computers. A device or program that executes instructions described by that ISA, s ...
into another.
See also
*
Wrapper library
*
Driver wrapper
*
Adapter pattern
*
Decorator pattern
*
Delegation (programming)
*
Forwarding (object-oriented programming)
*
Language binding wrapper to another language
*
SWIG automatic wrapper generator
*
Nested function
In computer programming, a nested function (or nested procedure or subroutine) is a named function that is defined within another, enclosing, block and is lexically scoped within the enclosing block meaning it is only callable by name within t ...
*
Partial application
Partial may refer to:
Mathematics
*Partial derivative, derivative with respect to one of several variables of a function, with the other variables held constant
** ∂, a symbol that can denote a partial derivative, sometimes pronounced "partial ...
References
{{Reflist
Articles with example Java code
Subroutines