C++ Functions
C++ Functions
IN
C++
INTRODUCTION TO FUNCTIONS
A function groups a number of program
statements into a unit and gives it a name.
This unit can then be invoked from other parts of
the
Another reason to use functions is to reduce
program size.
Any sequence of instructions that appears in a
program more than once is a candidate for being
made into a function. program.
FLOW OF CONTROL TO A FUNCTION
#include <iostream>
void starline(); //function declaration/ (prototype)
int main()
{
starline(); //call to function
cout << “Data type Range” << endl;
starline(); //call to function
cout << “char -128 to 127” << endl
starline(); //call to function
return 0;
}
// function definition
void starline() //function declarator/definition
{
for(int j=0; j<45; j++) //function body
cout << ‘*’;
}
THE FUNCTION DECLARATION
The declaration tells the compiler that at some
later point we plan to present a function called
starline.
The keyword void specifies that the function has
no return value, and the empty parentheses
indicate that it takes no arguments.
Function declarations are also called prototypes
CALLING THE FUNCTION
The function is called (or invoked, or executed)
three times from main().
Each of the three calls looks like this:
starline();
This is all we need to call the function: