codescracker


c++

C++ Function Prototype & Definition



« Previous Tutorial Next Tutorial »


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 :

int absval(int a);
int gcd(int n1, int n2);

Therefore, you can say that a function prototype has the following parts :

Let's look at the following function prototype :

int sum(int n1, int n2);

Here,

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:

void function_name(parameter list);

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:

return_type function_name(void);

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:

return_type function_name(parameter list)
{
	body of the function
}

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 :

int absval(int a)   // functions headers
{
	return (a < 0 ? -a : a) ;       //body of function absval

Here is another function definition example. This function has return type as int, and have two integer parameters

int gcd(int n1, int n2)
{
	// return the greatest common divisor
	
	int temp;
	while(n2)
	{
		temp = n2 ;
		n2 = n1%n2 ;
		n1 = temp ;
	}
	return n1 ;
}

From the above examples it is clear now that the parameter declaration list for a function takes this general form :

function_name(type varname1, type varname2, ......, type varnamen)

A C++ function can also have an 'open' parameter list i.e., it can have any number as follows :

type function_name(...)

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.

/* C++ Function Prototype and C++ Function Definition */

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

int add(int, int);         // function prototype
int subtract(int, int);    // function prototype
int multiply(int, int);    // function prototype
int divide(int, int);      // function prototype

void main()
{
	clrscr();
	int a, b;
	cout<<"Enter any two number: ";
	cin>>a>>b;
	cout<<"\nSummation = "<<add(a, b);
	cout<<"\nSubtraction = "<<subtract(a, b);  
	cout<<"\nMultiplication = "<<multiply(a, b); 
	cout<<"\nDivision = "<<divide(a, b); 
	getch();
}

int add(int x, int y)     // function definition
{
	int res;
	res = x + y;
	return res;
}

int subtract(int x, int y)    // function definition
{
	int res;
	res = x - y;
	return res;
}

int multiply(int x, int y)    // function definition
{
	int res;
	res = x * y;
	return res;
}

int divide(int x, int y)    // function definition
{
	if(y==0)
	{
		cout<<"\n\nDivide by Zero Error..!!";
		cout<<"\nPress any key to exit...";
		getch();
		exit(1);
	}
	else
	{
		int res;
		res = x / y;
		return res;
	}
}

Below are the two sample run of this C++ program:

c++ function prototype

c++ function definition

Here is another example, also demonstrating function prototype and function definition in C++. This program uses return_type as void

/* C++ Function Prototype and C++ Function Definition */

#include<iostream.h>
#include<conio.h>

void msg(void);   // function prototype

void main()
{
	clrscr();
	msg();
	getch();
}
void msg(void)    // function definition
{
	cout<<"Welcome to codescracker.com\n";
	cout<<"This is C++ Function Prototype & Definition Tutorial";
}

Here is the sample run of the above C++ program:

c++ function prototype function definition

Let's take one more example program for the complete understanding on C++ function prototype and function definition

/* C++ Function Prototype and C++ Function Definition */

#include<iostream.h>
#include<conio.h>

void msg1(void);  // function prototype
void msg2(void);  // function prototype
void msg3(void);  // function prototype

void main()
{
	clrscr();
	msg1();
	msg2();
	msg3();
	getch();
}

void msg1(void)   // function definition
{
	cout<<"Welcome to codescracker.com\n";
}

void msg2(void)   // function definition
{
	cout<<"You are learning C++ Functions Prototype & Definition\n";
}

void msg3(void)   // function definition
{
	cout<<"This program demonstrates C++ Function Prototype\n";
	cout<<"This program also demonstrates C++ Function Definition\n";
}

Below is the sample run of the above C++ program:

function prototype c++

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test