In C++, a function must be declared and defined before it is used (called) anywhere in the program.
C++ Function Prototype
A function prototype is a declaration of the function that tells the program about the type of the value returned by the function and the number and type of arguments.
Function prototyping is one very useful feature of C++ function. A function prototype describes the function interface to the compiler by giving details such as the number and type of arguments and the type of return values.
The prototype declaration looks just like a function definition except that it has no body i.e., its code is missing. This is the time you knew the difference between a declaration and a definition.
A declaration introduces a (function) name to the program whereas a definition is a declaration that also tells the program what the function is doing and how it is doing.
Thus the above given examples are function definitions and the following are declarations or shall we say, function prototypes :
Therefore, you can say that a function prototype has the following parts :
- return type
- name of the function
- argument list
Let's look at the following function prototype :
- return type - int
- name of the function - sum
- argument list - (int n1, int n2)
Since every function prototype is followed by a semicolon, so at last there will be ; as in the above function prototype.
Tip - A function declaration can skip the argument names but a function definition, can not.
Use of Void
As you know about void data type that it specifies an empty set of values and it is used as the return type for functions that do not return a value. Thus, a function that does not return a value is declared as follows:
By declaring a function's return type void, one makes sure that the function cannot be used in an assignment statement.
Tip - If a function does not return a result, declare the result type as void.
A function that does not require any parameter (i.e., it has an empty argument list) can be declared as follows:
Tip - If a function takes no argument, you should specify void in its prototype.
As already mentioned if you omit the type specifier of a function, it is assumed to be returning int values. For the functions returning non-integer values, the type specifier must be given.
A function prototype can either appear before the definition of calling the function (such prototypes are known as a global prototypes) or within the definition of calling function (such prototypes are known as local prototypes). The global and local prototypes have been described in separate tutorial, in C++ Scope Rules tutorial.
C++ Function Definition
The general form of a function definition is as given below:
Here, the return_type specifies the type of value that the return statement of the functions returns. It may be any valid C++ data type. If no type is specified, the compiler assumes that the function returns an integer value. The parameter list is a common-separated list of variables of a function referred to as its arguments. A function may be without any parameters, in which case, the parameter list is empty. Here are some examples of functions. Below is a function having only one parameter of int type and return type also as int :
Here is another function definition example. This function has return type as int, and have two integer parameters
From the above examples it is clear now that the parameter declaration list for a function takes this general form :
A C++ function can also have an 'open' parameter list i.e., it can have any number as follows :
C++ Function Prototype & Function Definition Example
Let's take an example program, demonstrating function prototype and function definition in C++. This program also uses function calling, which is covered in C++ Calling Function tutorial coming in the next chapter.
Below are the two sample run of this C++ program:
Here is another example, also demonstrating function prototype and function definition in C++. This program uses return_type as void
Here is the sample run of the above C++ program:
Let's take one more example program for the complete understanding on C++ function prototype and function definition
Below is the sample run of the above C++ program:
C++ Online Test
« Previous TutorialNext Tutorial »
- Is IT better than CS
- How do you become a minister
- What makes summer babies interesting
- How are small businesses using social media
- Why is the media unfair to India
- How is Sweden doing without cash
- How many types of hockey are there
- Are walk in showers worth the money
- Can a circuit breaker partially trip
- In Java what is multiple inheritance
- Which popular rappers are overweight
- Is breathing everyday normal
- How often do you admit youre wrong
- Is HDMI the standard today
- What cyber crimes have you done
- Are bullies products of nature or nurture
- Why is Slovakia so religious
- Was Atticus Finch a racist
- Should I leave IT job