Computer Science    
   
Table of contents
(Prev) Type punningTyped assembly language (Next)

Type signature

In computer science, a type signature or type annotation defines the inputs and outputs for a function, subroutine or method. A type signature includes at least the function name and the number of its arguments. In some programming languages, it may also specify the function's return type, the types of its arguments, or errors it may pass back.

Contents

Examples

C/C++

In C and C++ the type signature is declared by what is commonly known as a function prototype. In C/C++, a function declaration reflects its use; for example, a function pointer that would be invoked as:

char c;double d;int retVal = (*fPtr)(c, d);

has the signature:

(int) (char, double);

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 the Haskell programming language is written, generally, in the following format:

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, which is made possible by Haskell's support for first-class functions. That is, given a function that takes in two inputs, but had one argument supplied, the function is "curried" to produce a function of one argument—the one that is not supplied. Thus calling f(x), where f :: a -> b -> c, yields a new function f2 :: b -> c that can be called f2(b) to produce c.

The actual type specifications can consist of an actual type, such as Integer, or a general type variable that is used in parametric polymorphic functions, such as a, or b, or anyType. So we can write something like: functionName :: a -> a -> ... -> a

Since Haskell supports higher-order functions, functions can be passed as arguments. This is written as: functionName :: (a -> a) -> a

This function takes in a function with type signature a -> a, and returns data of type a out.

Java

In the Java virtual machine, internal type signatures are used to identify methods and classes at the level of the virtual machine code.

Example: The method String String.substring(int, int) is represented as Ljava/lang/String/substring(II)Ljava/lang/String;. The signature of main() method looks like this:

public static void main(String args[])

The method signature for the main() method contains three modifiers:

  • public indicates that the main() method can be called by any object,
  • static indicates that the main() method is a class method,
  • void indicates that the main() method has no return value.

Signature

The signature of a function is a way of describing the parameters and parameter types with which a legal call to the function can be made. It contains the name of the function, its parameters and their type, and in some cases the type of the return value. Signatures may also be referred to as "function signatures", "method signatures", or "prototype definitions"[citation needed].

The notion of a function signature is an important concept for all computer science undergraduates

  • Modern Object Orientation techniques make much use of Interfaces which are essentially a template made from function signatures
  • C++ teaching must cover the important concept of function overloading which involves first introducing function signatures.

The practice of multiple inheritance requires careful consideration of the function signatures to avoid unpredictable results.

Computer science theory and the teaching of polymorphism in particular makes much use of the concept of function signature.

Roughly equivalent to its prototype definition in the C programming language.

CS, like many sciences, reuses terminology frequently. If you encounter the term 'signature' in an area of CS not concerned with programming then be sure to check your terminology with the published texts to avoid confusion.

Other areas of Computer Science where you might encounter the term signature:

  • The study of databases might also involve the use of the term 'signature', however, before confusing the two things be sure of the definition at the top of this article.

In the ML family of programming languages, signature is a keyword referring to a construct of the module system that plays the role of an interface.

Method Signature

In computer programming, especially object-oriented programming, a method is commonly identified by its unique method signature, which usually includes the method name, and the number, types and order of its parameters.[1] 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 <stdarg.h>.

C#

Similar to the C syntax, C# sees as the method signature its name and the number and type of its parameters, where the last parameter may comprise of an array of values:[2]

void Add(out int sum, params int[] value);[...]Add(out sum, 3, 5, 7, 11, -1);  // sum == 25

Java

In the Java programming language, a method signature is the method name and the number and type of its parameters. Return types and thrown exceptions are not considered to be a part of the method signature.

methodName(parameters) {...};

For example, the following two methods have distinct signatures:

doSomething(int y);doSomething(String y);

The following three methods do have the same signatures and are considered the same, as only the return value differs. The name of the parameter is not part of the method signature and is ignored by the compiler for checking method uniqueness.

int doSomething(int y) String doSomething(int x)int doSomething(int z) throws java.lang.Exception

Objective-C

In the Objective-C programming language, method signatures for an object are declared in the interface header file. For example,

- (id)initWithInt:(int)value;

defines a method initWithInt that returns a general object (an id) and takes one integer argument. Objective-C only requires a type in a signature to be explicit when the type is not id; this signature is equivalent:

- initWithInt:(int)value;

References

  1. ^ Paul Leahy. "Method Signature". http://www.about.com/: About.com Guide. Retrieved 2011-05-31. "A method signature is part of the method declaration. It is the combination of the method name and the parameter list." 
  2. ^ Mössenböck, Hanspeter (2002-03-25). "Advanced C#: Variable Number of Parameters". http://ssw.jku.at/Teaching/Lectures/C Sharp/Tutorial/: Institut für Systemsoftware, Johannes Kepler Universität Linz, Fachbereich Informatik. p. 52. Retrieved 2011-08-03. 
(Prev) Type punningTyped assembly language (Next)