# Wrapper function

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Wrapper_function
> Markdown URL: https://mediated.wiki/source/Wrapper_function.md
> Source: https://en.wikipedia.org/wiki/Wrapper_function
> Source revision: 1350540621
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Function whose job is only to call another subroutine

This article is about programming structure. For wrapping in graphics, see [Wrapping (graphics)](/source/Wrapping_(graphics)).

"Wrapper method" redirects here. For wrapper methods in feature selection, see [Feature selection § Wrapper method](/source/Feature_selection#Wrapper_method).

A **wrapper function** is a [function](/source/Function_(computer_programming)) (another word for a *subroutine*) in a [software library](/source/Library_(computing)) or a [computer program](/source/Computer_program) whose main purpose is to call a second subroutine[1] or a [system call](/source/System_call) 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](/source/Delegation_(programming)) 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](/source/Java_AWT) library demonstrate this.[2] 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

Main article: [Adapter pattern](/source/Adapter_pattern)

Wrapper functions can be used to [adapt](/source/Adapter_pattern) 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](/source/Debugger) 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.[3] 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](/source/Regression_testing). 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](/source/Programming_language) that does not support [multiple inheritance](/source/Multiple_inheritance) of base classes, wrapper functions can be used to simulate it. Below is an example of part of a [Java](/source/Java_(programming_language)) class that "inherits" from java.util.LinkedList and java.util.HashSet. See [method](/source/Method_(computer_programming)) for further implementation details.

import java.util.HashSet;
import java.util.LinkedList;

public class Test implements LinkedList, HashSet {
    @Override
    // contains data members and data methods
    // covariant return
}

## Library functions and system calls

Many [library](/source/Standard_library) functions, such as those in the [C Standard Library](/source/C_Standard_Library), act as [interfaces](/source/Interface_(computing)) for [abstraction](/source/Abstraction_(computer_science)) of [system calls](/source/System_call). The *fork* and *execve* functions in [glibc](/source/Glibc) are examples of this. They call the [lower-level](/source/Low-level) *[fork](/source/Fork_(operating_system))* and *[execve](/source/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.[4][5]

## Helper function

A helper function is a function which groups parts of [computation](/source/Computation) by assigning descriptive names and allowing for the reuse of the computations.[6] 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](/source/Binary_translation#Dynamic_binary_translation), in which helper functions of a particular [architecture](/source/Computer_architecture) are used in translation of [instructions](/source/Machine_code) from one [instruction set](/source/Instruction_set_architecture) into another.[7]

## See also

- [Wrapper library](/source/Wrapper_library)

- [Driver wrapper](/source/Driver_wrapper)

- [Adapter pattern](/source/Adapter_pattern)

- [Decorator pattern](/source/Decorator_pattern)

- [Delegation (programming)](/source/Delegation_(programming))

- [Forwarding (object-oriented programming)](/source/Forwarding_(object-oriented_programming))

- [Language binding](/source/Language_binding) wrapper to another language

- [SWIG](/source/SWIG) automatic wrapper generator

- [Nested function](/source/Nested_function)

- [Partial application](/source/Partial_application)

## References

1. **[^](#cite_ref-1)** Reselman, Bob; Peasley, Richard; Pruchniak, Wayne (1998). [*Using Visual Basic 6*](https://books.google.com/books?id=X5ZQAAAAMAAJ). Que. p. 446. [ISBN](/source/ISBN_(identifier)) [9780789716330](https://en.wikipedia.org/wiki/Special:BookSources/9780789716330).

1. **[^](#cite_ref-2)** [The Java Tutorials](https://java.sun.com/docs/books/tutorial/uiswing/events/mouselistener.html)

1. **[^](#cite_ref-3)** Stevens, Richard; Fenner, Bill; Rudoff; Andrew M. (2003). [*UNIX Network Programming*](https://books.google.com/books?id=ptSC4LpwGA0C). Addison-Wesley. pp. 5–6, 29. [ISBN](/source/ISBN_(identifier)) [9780131411555](https://en.wikipedia.org/wiki/Special:BookSources/9780131411555).

1. **[^](#cite_ref-4)** ["syscalls(2) - Linux manual page"](https://man7.org/linux/man-pages/man2/syscalls.2.html). *man7.org*. Retrieved 2020-04-25.

1. **[^](#cite_ref-5)** ["System Calls (The GNU C Library)"](https://www.gnu.org/software/libc/manual/html_node/System-Calls.html). *www.gnu.org*. Retrieved 2020-04-25.

1. **[^](#cite_ref-6)** Fisler, Kathi (2005). ["CS 1101: Helper functions"](https://web.cs.wpi.edu/~cs1101/a05/Docs/creating-helpers.html). [Worcester Polytechnic Institute](/source/Worcester_Polytechnic_Institute). [Archived](https://web.archive.org/web/20210507033146/https://web.cs.wpi.edu/~cs1101/a05/Docs/creating-helpers.html) from the original on 7 May 2021. Retrieved 17 November 2021.

1. **[^](#cite_ref-7)** Wang, Wenwen (3 March 2021). [*Helper function inlining in dynamic binary translation*](https://dl.acm.org/doi/10.1145/3446804.3446851). CC: Compiler Construction. New York, United States: [Association for Computing Machinery](/source/Association_for_Computing_Machinery). p. 107. [doi](/source/Doi_(identifier)):[10.1145/3446804.3446851](https://doi.org/10.1145%2F3446804.3446851). [ISBN](/source/ISBN_(identifier)) [978-1-4503-8325-7](https://en.wikipedia.org/wiki/Special:BookSources/978-1-4503-8325-7).

---
Adapted from the Wikipedia article [Wrapper function](https://en.wikipedia.org/wiki/Wrapper_function) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Wrapper_function?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
