Conceptintro
Function signature
Also called signature, function prototype, method signature, type signature, call signature.
A function signature is the outside of a function: its name, its parameters with their types and its result type. Callers need only the signature, and a typed language checks each call against it.
- Applies to
- core-languages
Idea
A function has an outside and an inside. The outside is the signature: the name, the parameters and their types, and the type of the result. The inside is the body. A caller needs to know only the signature to call the function correctly.
function repeatText(text: string, times: number): string {
return text.repeat(times);
}
console.log(repeatText("ab", 3));
ababab
The first line is the signature. It says that repeatText takes a string and a number, and gives back a string. A reader can use the function from this line alone, and the compiler rejects a call such as repeatText(3, "ab").
The Rust Reference says that a function can declare "a set of input variables as parameters, through which the caller passes arguments into the function, and the output type of the value the function will return to its caller on completion"1. These parts are the signature.
How it works
- The declaration. The program writes the signature as the first part of the function definition. In C and C++, the program can also write the signature alone, without a body, as a declaration.
- The check at the call. A compiler that knows the signature checks each call. In C, a declaration with parameter types "serves as a function prototype for any future function call expressions, forcing conversions from argument expressions to the declared parameter types and compile-time checks for the number of arguments"2.
- The check of the body. The compiler also checks that the body agrees with the signature, for example that each
returngives a value of the result type. - Identity. A language with overloading uses the signature to tell two functions with the same name apart. "The signature of a function consists of its name and parameter-type-list" in C++3.
In languages
| Language | Verdict | Note |
|---|---|---|
| Python | Native | |
| C++ | Native | |
| Java | Native | |
| C | Native | |
| C# | Native | |
| JavaScript | Native | |
| TypeScript | Native | |
| Go | Native | |
| Rust | Native | |
| Kotlin | Native | A Kotlin function declares the type of each parameter and the return type after a colon. No example file exists because kotlinc is not installed. |
| SQL | Native | CREATE FUNCTION declares the name, the typed parameters and the RETURNS type of a function. No example file exists. |
| Bash | Absent | |
| PowerShell | Native | A PowerShell function can declare typed parameters in a param block, and the OutputType attribute declares the result type. No example file exists because pwsh is not installed. |
| HTML | Does not apply | HTML is a markup language and has no functions. Scripts in a page use JavaScript. |
| CSS | Does not apply | CSS cannot define functions in a standard module, so it has no function signatures. |
| Regular expressions | Does not apply | A regular expression has no functions, so it has no signatures. |
- Python. The parameters have no required types. "Parameters may have an annotation of the form “: expression” following the parameter name."4 "Functions may have “return” annotation of the form “-> expression” after the parameter list."4 Tools such as type checkers read these annotations, but Python does not check them when it runs.
- JavaScript. A signature has only the name and the parameter names. The language does not check the number or the types of the arguments.
- TypeScript. TypeScript adds types to the JavaScript signature. A function type expression such as
(a: string) => voidmeans "“a function with one parameter, named a, of type string, that doesn’t have a return value”"5. An object type can also have a call signature. - Java. The JLS defines the signature without the result type: "Two methods or constructors, M and N, have the same signature if they have the same name, the same type parameters (if any)" and the same formal parameter types6.
- C#. The C# guide lists "the access level such as public or private, optional modifiers such as abstract or sealed, the return value, the name of the method, and any method parameters. These parts together are the signature of the method."7 But "A return type of a method is not part of the signature of the method for the purposes of method overloading."7
- C. A declaration such as
int sum(int a, int b);is a prototype. A declaration with no parameter list "does not introduce a prototype and any future function call expressions will perform default argument promotions and will invoke undefined behavior if the number of arguments doesn't match the number of parameters"2. - C++. "Every function has a signature. The signature of a function consists of its name and parameter-type-list."3 For a member function, the signature "contains the class of which the function is a member"3.
- Go. The grammar calls the parameters and the results the signature. "A function type denotes the set of all functions with the same parameter and result types."8
- Rust. Each parameter and the result must have a type. If the output type is not written, "it is the unit type"1.
- Bash. A Bash function has no signature. It is declared as "fname () compound-command [ redirections ]"9, and the parentheses are always empty. The body reads its arguments as
$1,$2and so on.
def average(a: int, b: int) -> float:
return (a + b) / 2
print(average(3, 4))
3.5
- Verdict: native. The signature has the parameter names and optional annotations. Python does not check the annotations when the program runs.
#include <iostream>
double average(int a, int b);
int main() {
std::cout << average(3, 4) << "\n";
}
double average(int a, int b) {
return (a + b) / 2.0;
}
3.5
- Verdict: native. The declaration before
maingives the signature, and the definition aftermaingives the body.
public class Main {
static double average(int a, int b) {
return (a + b) / 2.0;
}
public static void main(String[] args) {
System.out.println(average(3, 4));
}
}
3.5
- Verdict: native. The result type comes before the name, and each parameter has a type. The JLS signature for overloading has the name and the parameter types.
#include <stdio.h>
static double average(int a, int b);
int main(void) {
printf("%.1f\n", average(3, 4));
return 0;
}
static double average(int a, int b) {
return (a + b) / 2.0;
}
3.5
- Verdict: native. The first line of the program is a prototype: the signature with no body. The compiler checks the call in
mainagainst it.
using System;
using System.Globalization;
class Program
{
static double Average(int a, int b)
{
return (a + b) / 2.0;
}
static void Main()
{
Console.WriteLine(Average(3, 4).ToString(CultureInfo.InvariantCulture));
}
}
3.5
- Verdict: native. The return type comes before the name, and each parameter has a type.
function average(a, b) {
return (a + b) / 2;
}
console.log(average(3, 4));
3.5
- Verdict: native. The signature has only the name and the parameter names. JavaScript does not check the number or the types of the arguments.
function average(a: number, b: number): number {
return (a + b) / 2;
}
console.log(average(3, 4));
3.5
- Verdict: native. The signature gives the types of the parameters and the result, and the compiler checks each call against it.
package main
import "fmt"
func average(a, b int) float64 {
return float64(a+b) / 2
}
func main() {
var f func(int, int) float64 = average
fmt.Println(f(3, 4))
}
3.5
- Verdict: native. The parameter types follow the names, and the result type follows the parameter list. The type of
averageisfunc(int, int) float64.
fn average(a: i32, b: i32) -> f64 {
f64::from(a + b) / 2.0
}
fn main() {
println!("{}", average(3, 4));
}
3.5
- Verdict: native. Each parameter must have a type, and
->gives the output type.
sum() {
echo $(( $1 + $2 ))
}
sum 3 4
7
- Verdict: absent. A Bash function has no signature: the parentheses in the definition are always empty, and the body reads its arguments as
$1and$2. Bash arithmetic has only integers, so this example adds the two numbers.
Variations and related ideas
- Signatures as contracts. A signature is a small contract between the function and its callers: the callers give arguments of the parameter types, and the function gives a result of the result type. It says nothing about the effects of the function or the rules on the values, so documentation and tests must give the rest.
- Function types. In Go, TypeScript and Rust, the signature without the name is a type. A variable or a parameter of that type can hold any function with the same parameter and result types8. See Higher-order function.
- Signatures and overloading. Java, C#, and C++ allow several functions with one name if the signatures differ. The result type does not tell them apart67.
- Generic signatures. A signature can have type parameters, such as
fn first<T>(items: &[T]) -> &Tin Rust. See Generics. - Method signatures. A method signature also names the type that the method belongs to. See Method.
- Prototypes and headers. C and C++ put the signatures of a library in header files, so other files can call the functions and the compiler can check the calls.
Pitfalls
Type annotations are not checked at run time in Python
A Python signature with annotations looks like a typed signature, but "The presence of annotations does not change the semantics of a function."4
This code runs but gives a wrong answer on purpose.
def repeat_text(text: str, times: int) -> str:
return text * times
print(repeat_text(3, 4))
12
The call passes two integers, and Python runs it with no error. Run a type checker such as mypy on the code to find this mistake before the program runs.
A callback with a different signature
JavaScript does not compare the signature of a callback with the arguments that the caller gives. MDN shows a well-known case10:
This code runs but gives a wrong answer on purpose.
console.log(["1", "2", "3"].map(parseInt));
[ 1, NaN, NaN ]
"Array.prototype.map passes 3 arguments: the element, the index, and the array."10 parseInt takes two: the text and the radix. "The third argument is ignored by parseInt — but not the second one!"10 So the second call is parseInt('2', 1), which gives NaN. Pass a function with the right signature: ['1', '2', '3'].map((s) => parseInt(s, 10)).
Cost
A signature costs nothing at run time. It is information for the compiler and for the reader. In a typed language, the compiler uses it to find wrong calls before the program runs.
History
Early C declared a function without the types of its parameters. Wikipedia says: "In early versions of C, if a function was not previously declared and its name occurred in an expression followed by a left parenthesis, it was implicitly declared as a function that returns an int and nothing was assumed about its arguments."11
The C standard of 1989 added parameter types to declarations and definitions. cppreference calls the form with typed parameters the "New-style (C89) function definition", and the form without them the "Old-style (K&R) function definition"12. A declaration with parameter types is a prototype, and the compiler checks each call against it2.
References
- Functions, The Rust Reference, items.fn.intro; items.fn.signature
- Function declarations, C reference, cppreference.com, Explanation
- Function declaration, C++ reference, cppreference.com, Function signature
- Compound statements, The Python Language Reference, 8.7 Function definitions
- More on Functions, The TypeScript Handbook, Function Type Expressions; Call Signatures
- Classes, The Java Language Specification, Java SE 25 Edition, 8.4.2 Method Signature
- Methods, C# Programming Guide, Method signatures
- The Go Programming Language Specification, Function types
- Shell Functions, Bash Reference Manual, 3.3 Shell Functions
- Array.prototype.map(), MDN Web Docs, Using parseInt() with map()
- Function prototype, Wikipedia, the introduction; Uses
- Function definitions, C reference, cppreference.com, Syntax
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag R151YT Edit this page History Report an error Markdown