Function prototype
≈ 5 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In computer programming, a function prototype 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 function definition specifies how the function does what it does (the "implementation"), a function prototype merely specifies its interface, i.e. what data types go in and come out of it. The term "function prototype" is particularly used in the context of the programming languages C and C++ where placing forward declarations of functions in header files allows for splitting a program into translation units, i.e. into parts that a compiler can separately translate into object files, to be combined by a linker into an executable or a library. The function declaration precedes the function definition, giving details of name, return type, and storage class along with other relevant attributes. Function prototypes can be used when either: * Defining an external type * Creating an interface part In a prototype, parameter names are optional (and in C/C++ have function prototype scope, meaning their scope ends at the end of the prototype), however, the type is necessary along with all modifiers (e.g. if it is a pointer or a reference to parameter) except alone. In object-oriented programming, interfaces and abstract methods serve much the same purpose.
In computer programming, a function prototype 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 function definition specifies how the function does what it does (the "implementation"), a function prototype merely specifies its interface, i.e. what data types go in and come out of it. The term "function prototype" is particularly used in the context of the programming languages C and C++ where placing forward declarations of functions in header files allows for splitting a program into translation units, i.e. into parts that a compiler can separately translate into object files, to be combined by a linker into an executable or a library. The function declaration precedes the function definition, giving details of name, return type, and storage class along with other relevant attributes. Function prototypes can be used when either: Defining an external type Creating an interface part In a prototype, parameter names are optional (and in C/C++ have function prototype scope, meaning their scope ends at the end of the prototype), however, the type is necessary along with all modifiers (e.g. if it is a pointer or a reference to parameter) except alone. In object-oriented programming, interfaces and abstract methods serve much the same purpose. void sum(int a, int b); // without named parameters: void sum(int, int); // with trailing return types (C++): auto sum(int, int) -> void; Function prototypes include the function signature, the name of the function, return type and access specifier. In this case the name of the function is "sum". The function signature defines the number of parameters and their types. The return type is "void". This means that the function is not going to return any value. Note that the parameter names in the first example are optional. include include // Function prototype char orderingCharacter(int n); int main() { putchar(orderingCharacter(-1)); // Correctly formatted call putchar(orderingCharacter(1.5)); // Compiler warning: type mismatch putchar(orderingCharacter("Hello")); // Compiler warning: type mismatch putchar(orderingCharacter()); // Compiler error: too few arguments // Although adding 1 to INT_MAX is an integer overflow error, // it cannot be detected at compile time putchar(orderingCharacter(INT_MAX + 1)); } // Function definition char orderingCharacter(int n) { if (n > 0) { return '>'; } else if (n The function orderingCharacter() expects to be called with an integer argument. By including the function prototype, you inform the compiler that the function takes one integer argument and you enable the compiler to catch incorrectly specified calls. interface PaymentProcessor { boolean processPayment(double amount, String currency); String getReceiptDetails(); void refundPayment(String transactionId); } class CreditCardProcessor implements PaymentProcessor { private double lastAmount; private String lastTransactionId; @Override public boolean processPayment(double amount, String currency) { this.lastAmount = amount; this.lastTransactionId = "TXN" + System.currentTimeMillis(); System.out.printf("Processing credit card payment of %f %s...%n", amount, currency); return true; } @Override public String getReceiptDetails() { return String.format("Receipt for ID %s | Amount: $%f", lastTransactionId, lastAmount); } @Override public void refundPayment(String transactionId) { System.out.printf("Refunding transaction: %s...%n", transactionId); } }
Text derived from https://en.wikipedia.org/w/index.php?oldid=1361304104 under CC BY-SA 4.0; modifications have been made.