GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||
GeezerVison is showing en.wikipedia.org — Function prototype - Wikipedia (Redirected from Function declaration) This article is about function declarations. For the prototype property of JavaScript functions and objects, see JavaScript § Object-orientation (prototype-based). For other uses, see Software prototyping.
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.[1] Function prototypes can be used when either:[2]
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 const parameter) except const alone. In object-oriented programming, interfaces and abstract methods serve much the same purpose. ExampleConsider the following function prototype: 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. UsesIn 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 #include <limits.h> #include <stdio.h> // 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 < 0) { return '<'; } return '='; } The function Creating library interfacesBy placing function prototypes in a header file, one can specify an interface for a library. Class declarationIn C++, function prototypes are also used in class definitions. In Java and C#, an interfaces are declared with signatures, which are then implemented by the implementing class. 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); } } See alsoReferences
| ||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |