In
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, a type signature or type annotation defines the inputs and outputs for a function,
subroutine
In computer programming, a function or subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.
Functions may ...
or
method
Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to:
*Scien ...
. A type signature includes the number, types, and order of the arguments contained by a function. A type signature is typically used during
overload resolution for choosing the correct definition of a function to be called among many overloaded forms.
Examples
C/C++
In
C and
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, the type signature is
declared by what is commonly known as a
function prototype
In computer programming, a function prototype or function interface is a declaration of a function that specifies the function’s name and type signature (arity, data types of parameters, and return type), but omits the function body. While a f ...
. In C/C++, a function
declaration reflects its use; for example, a
function pointer
A function pointer, also called a subroutine pointer or procedure pointer, is a pointer that points to a function. As opposed to referencing a data value, a function pointer points to executable code within memory. Dereferencing the function poin ...
with the signature would be called as:
char c;
double d;
int retVal = (*fPtr)(c, d);
Erlang
In
Erlang, type signatures may be optionally declared, as:
-spec(function_name(type1(), type2(), ...) -> out_type()).
For example:
-spec(is_even(number()) -> boolean()).
Haskell
A type signature in
Haskell
Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lang ...
generally takes the following form:
functionName :: arg1Type -> arg2Type -> ... -> argNType
Notice that the type of the result can be regarded as everything past the first supplied argument. This is a consequence of
currying
In mathematics and computer science, currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. For example, currying a function f that ...
, which is made possible by Haskell's support for
first-class function
In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from ...
s; this function requires two inputs where one argument supplied and the function is "curried" to produce a function for the argument not supplied. Thus calling , where , yields a new function that can be called to produce .
The actual type specifications can consist of an actual type, such as , or a general
type variable In type theory and programming languages, a type variable is a mathematical variable ranging over types. Even in programming languages that allow mutable variables, a type variable remains an abstraction, in the sense that it does not correspond t ...
that is used in
parametric polymorphic functions, such as , or , or . So we can write something like:
Since Haskell supports
higher-order function
In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following:
* takes one or more functions as arguments (i.e. a procedural parameter, which is a parameter of a procedure that is itself ...
s, functions can be passed as arguments. This is written as:
This function takes in a function with type signature and returns data of type out.
Java
In the
Java virtual machine
A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally describes ...
, ''internal type signatures'' are used to identify methods and classes at the level of the virtual machine code.
Example: The method is represented in
bytecode
Bytecode (also called portable code or p-code) is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references (norma ...
as .
The signature of the method looks like this:
public static void main(String[] args);
And in the disassembled bytecode, it takes the form of
The method signature for the method contains three modifiers:
* indicates that the method can be called by any object.
* indicates that the method is a class method.
* indicates that the method has no return value.
Signature
A function signature consists of the function prototype. It specifies the general information about a function like the name, scope and parameters. Many programming languages use
name mangling
In compiler construction, name mangling (also called name decoration) is a technique used to solve various problems caused by the need to resolve unique names for programming entities in many modern programming languages.
It provides a way of e ...
in order to pass along more semantic information from the compilers to the linkers. In addition to mangling, there is an excess of information in a function signature (stored internally to most compilers) which is not readily available, but may be accessed.
Understanding the notion of a function signature is an important concept for all computer science studies.
* Modern object orientation techniques make use of
interfaces
Interface or interfacing may refer to:
Academic journals
* Interface (journal), ''Interface'' (journal), by the Electrochemical Society
* ''Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Lin ...
, which are essentially templates made from function signatures.
* C++ uses
function overloading
In some programming languages, function overloading or method overloading is the ability to create multiple functions of the same name with different implementations. Calls to an overloaded function will run a specific implementation of that f ...
with various signatures.
The practice of
multiple inheritance
Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or ...
requires consideration of the function signatures to avoid unpredictable results.
Computer science theory, and the concept of
polymorphism in particular, make much use of the concept of function signature.
In the C programming language signature is roughly equivalent to its
prototype definition.
The term "signature" may carry other meanings in computer science:
*
File signature
{{short description, Data used to identify or verify the content of a file
In computing, a file signature is data used to identify or verify the contents of a file. In particular, it may refer to:
* File magic number: bytes within a file used to ...
s can identify or verify the content of a file.
* Database signatures can identify or verify the schema or a version of a database.
* In the
ML family of
programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming ...
s, "signature" is used as a keyword referring to a construct of the module system that plays the role of an
interface
Interface or interfacing may refer to:
Academic journals
* ''Interface'' (journal), by the Electrochemical Society
* ''Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Linguistics''
* '' Inte ...
.
Method signature
In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, especially
object-oriented programming
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
, a
method
Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to:
*Scien ...
is commonly identified by its unique method signature, which usually includes the method name, and the number, types and order of its
parameters
A parameter (), generally, is any characteristic that can help in defining or classifying a particular system (meaning an event, project, object, situation, etc.). That is, a parameter is an element of a system that is useful, or critical, when ...
. A method signature is the smallest
type of a method.
Examples
C/C++
In C/C++, the method signature is the method name and the number and type of its parameters, but it is possible to have a last parameter that consists of an array of values:
int printf(const char*, ... );
Manipulation of these parameters can be done by using the routines in the standard library header .
C#
Similar to the syntax of C, method signatures in
C# are composed of a name and the number and type of its parameters, where the last parameter may be an array of values:
void Add(out int sum, params int[] value);
[...]
Add(out sum, 3, 5, 7, 11, -1); // sum 25
Java
In Java (programming language), Java, a method signature is composed of a name and the number, type and order of its parameters. Return types and thrown exceptions are not considered to be a part of the method signature, nor are the names of parameters; they are ignored by the compiler for checking method uniqueness.
The method signatures help distinguish overloaded methods (methods with the same name) in a class. Return types are not included in overloading. Only method signatures should be used to distinguish overloaded methods.
For example, the following two methods have different signatures:
void doSomething(String[] x); // doSomething(String[])
void doSomething(String x); // doSomething(String)
The following two methods both have the same signature:
int doSomething(int x); // doSomething(int)
void doSomething(int y) throws Exception; // doSomething(int)
Julia
In
Julia
Julia is usually a feminine given name. It is a Latinate feminine form of the name Julio and Julius. (For further details on etymology, see the Wiktionary entry "Julius".) The given name ''Julia'' had been in use throughout Late Antiquity (e.g. ...
, function signatures take the following form:
commission(sale::Int, rate::Float64)::Float64
The types in the arguments are used for the
multiple dispatch
Multiple dispatch or multimethods is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run-time (dynamic) type or, in the more general case, some other attribute of more than one of ...
. The return type is validated when the function returns a value, and a runtime exception is raised if the type of the value does not agree with the specified type.
Abstract types are allowed and is encouraged for implementing general behavior that is common to all subtypes. The above function can therefore be rewritten as follows. In this case, the function can accept any Integer and Real subtypes accordingly.
commission(sale::Integer, rate::Real)::Real
Types are completely optional in function arguments. When unspecified, it is equivalent to using the type Any, which is the super-type of all types. It is idiomatic to specify argument types but not return type.
Objective-C
In the
Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTS ...
programming language, method signatures for an object are declared in the interface header file. For example,
- (id)initWithInt:(int)value;
defines a method that returns a general object (an ) and takes one integer argument. Objective-C only requires a type in a signature to be explicit when the type is not ; this signature is equivalent:
- initWithInt:(int)value;
Rust
In
Rust
Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH ...
, function signatures take the following form:
fn commission(sale: u32, rate: f64) -> f64;
See also
*
*
References
{{DEFAULTSORT:Type Signature
Type theory
Subroutines