Function Declarations
A function declaration is statement of what a function's signature looks like. A function's signature is a combination of the function's name; the number, order, and types of its parameters; as well as any qualifiers that can be applied to the function. Sometimes function signatures are called "method signatures" because they belong to the member function of a class. A method is identical in all respects to a "normal" function except that a method always belongs to an object or class.
Every function in the same program must have a unique signature so the compiler knows which one is being called at any point in the code. When there are two or more functions in the same program that have the same name but have different signatures, the function is said to be overloaded. Because the function signatures are different, it therefore follows that the function declarations of overloaded functions are also different.
A function's declaration is different from its definition; although it is possible in some programming languages to declare and define a function at the same time. In ANSI C and C++ the compiler must at least know what a function's declaration is before it is called in the code. A typical C/C++ function declaration, typically located in a header file, might look like this:
- // file somefuncs.h
- int myFunction(int value);
And the definition, which would usually be in a separate source file, might look like this:
- // somefuncs.c
- int myFunction(int value) {
- /* do something with value and return a result */
- }
The function's declaration acts as the interface to it, and in a properly designed program it should be possible to change a function's implementation without breaking the code that calls it.
Ada and Smalltalk have a similar separation of declaration and definition, but the language syntax is very different. Java, however, combines declaration and definition in the same place; C++ allows for this, too, but it is not generally good practice in this language. The function "myFunction()" in Java might look like this:
- public class somefuncs {
- public int myFunction( int value ) {
- /* do something with value and return a result */
- }
- };
In the Java case, all functions are methods; that is, they are all associated with a class or an object. In C all functions are just functions, but C++ supports methods and functions.
As well as a name, a return type, and a list of parameters, a function can be declared with various modifiers associated with it. For example, in most object-oriented languages it is possible to determine a method's visibility to the outside world by declaring it as "private," "public," or "protected." The exact semantics of these visibility modifiers differs a bit between languages, but they are substantially the same.
For example, "public" method is, in both C++ and Java, visible to all other objects in the program. A "protected" item in C++ is visible only to objects of that specific type or objects of classes derived from that type. But in Java a "protected" member is visible only to objects of its own class, objects of subclasses, or within classes in the same package (a package is a logical collection of classes that have some possibly arbitrary common attributes). In both C++ and Java, "private" member items are visible only to the class they are defined in.
C, C++, Ada, Smalltalk, and Java all support the "static" modifier that also has slightly different semantics from language to language (and in both C and C++ it has different semantics depending on context!). When applied to a method, it means the method is bound to the class as a whole and not to a particular object of the class. When it is applied to an ordinary C or C++ function, it means that the function is visible only within the source file in which it is declared.
This is the complete article, containing 631 words
(approx. 2 pages at 300 words per page).