Home »
C++ programming language
Introduction of C++ functions
What is C++ function?
A function is a block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts as required. It also optionally returns a value to the calling function.
Need of function
A complex problem may be decomposed into a small or easily manageable parts or modules called functions. Functions are very useful to read, write, debug and modify complex programs They can also be incorporated in the main program. The main() itself is a function is invoking the other functions to perfom various tasks.
Parts of C++ function
A function has following parts :
- Function prototype
- Function definition
- Function calling
1) Function prototype
One of the most important features of C++ is the function prototypes. A function prototype tells the compiler the name of the function, the type of data returned by the function, the number of parameters the function expects to receive, the types of the parameters, and the order in which these parameters are expected.
The compiler use function prototypes to validate function calls.
function prototype is a declarative statement in the calling program.
return_type function_name(argument_list);
2) Function definition
A function definition specifies the name of the function, the types and number of parameters it expects to receive, and its return type. A function definition also includes a function body with the declarations of its local variables, and the statements that determine what the function does.
function prototype is a declarative statement in the calling program.
return_type function_name(argument_list)
{
statement 1;
.
.
.
statements n;
[return value];
}
**** Note : [return value] is an optional statement, if function return type is void then there is no need to return any value ..
3) Function calling
Function calling is a statement, which is used to call/execute/use function in your program/function.
When the function is called , control is transferred to the first statement in the function, the other statements in the function body are then executed and control returns to the calling program.
function_name(argument_list);
Example of C++ functions
Consider the example
#include <iostream>
using namespace std;
// declaration of functions
void printMessage(void);
float calAverage(int, int, int);
int main() {
int a, b, c;
float avg;
// calling first function
printMessage();
cout << "Enter first number :";
cin >> a;
cout << "Enter second number:";
cin >> b;
cout << "Enter third number :";
cin >> c;
// calling calAverage function
avg = calAverage(a, b, c);
cout << "\nAverage is = " << avg << "\n";
return 0;
}
/***********************************************
*function name : printMessage
*description : to print some message
*return type : No (void)
*argument list : No (void)
***********************************************/
void printMessage(void) { cout << "\nHello.. I am function's body !!!\n"; }
/***********************************************
*function name : calAverage
*description : to calculate average
*return type : int,int,int (3 integer values)
*argument list : float
***********************************************/
float calAverage(int x, int y, int z) {
int sum = 0;
sum = (x + y + z);
return ((float)sum / 3);
}
Output
Hello.. I am function's body !!!
Enter first number :11
Enter second number:13
Enter third number :34
Average is = 19.3333