Javatpoint Logo
Javatpoint Logo

Function Prototype in C++

A function prototype is a declaration of the function that informs the program about the number and kind of parameters, as well as the type of value the function will return. One incredibly helpful aspect of C++ functions is function prototyping. A function prototype provides information, such as the number and type of parameters and the type of return values, to explain the function interface to the compiler.

The prototype declaration resembles a function definition exactly, with the exception that it lacks a body, or its code. At this point, you were aware of the distinction between a statement and a definition.

A definition is a declaration that also informs the program what the function is doing and how it is doing, as opposed to a declaration, which simply introduces a (function) name to the program. Therefore, the examples above are function definitions, and the examples that follow are declarations, or perhaps a better term would be function prototypes:

Consequently, the components of a function prototype are as follows:

  • return type
  • name of the function
  • argument list

Let's look at the prototype for the following function:

Here,

  • return type - int
  • name of the function - add
  • argument list - (int a1, int a2)

Since a semicolon follows every function prototype, there will eventually be; just like in the previous function prototype.

Note: While a function definition cannot omit the parameter names, a function declaration can.

Usage of Void

As you are aware, the void data type is used as the return type for functions that do not return a value and describes an empty collection of values. Consequently, the declaration of a function that doesn't return a value is as follows:

One ensures that a function cannot be utilized in an assignment statement by defining the return type of the function to be void.

NOTE: Declare the result type as void if a function does not return a value.

A function can be defined as follows if it has no parameters and has an empty argument list:

NOTE: You should declare void in a function's prototype if it doesn't accept any arguments.

As was already noted, if a function's type specifier is omitted, it is presumed that it will return int values. The type specifier must be provided for functions yielding non-integer values.

A function prototype may exist either before or after the definition of invoking the function (these prototypes are referred to as global prototypes) (such prototypes are known as local prototypes). The C++ Scope Rules tutorial has a separate tutorial that describes both the global and local prototypes.

Let us look at an example of function prototype in c++:

OUTPUT:

5
???.
Process executed in 0.11 seconds
Press any key continue.

Explanation

In the above example the prototype is:

The function prototype feature in C++ allows us to call the function before it has been declared since in the example above, the compiler is given information about the function name and its parameters.

Another example to better understanding:

OUTPUT:

Welcome to JavaTpoint
?????????????.
Process executed 0.22 seconds
Press any key to continue.

Explanation

The function in the code above has a void return type, so it returns nothing. We have performed function prototyping, called the function, and then declared it, and as a result, we are receiving an output free of errors.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA