The basic building block to modularize a program, is – the function – by separating its tasks into self-contained units. Also known as methods or procedures. A function is a program construct, having a unique name, may accept parameter(s), consists of set by step well defined instructions, and return a value or simply transfer the control back to its invoker.

You have used functions in every program you have written. These functions are pre-packaged in the standard C++ library, called built-in functions. The functions written by a programmer are called user-defined functions or programmer – defined functions.

Benefits of Using Functions

  • Reusability: Statements are written only once, but can be used perhaps in several locations in a program
  • Hiding implementation details: The code of these functions is hidden from other functions
  • Divide and conquer: Makes program development manageable by constructing program from small, simple pieces
  • Avoid repeating code: It allows you to reuse the same code by just calling it where ever it is needed.
  • Easier debugging: This division makes the program easier to debug and maintain

To promote a program reusability, every function should be

  • Limited to performing a single task,
  • Well defined task
  • Having expressive name

Such functions make a program easier to write, test, debug, and maintain. If you cannot choose a concise name that expresses a function’s task, your function might be attempting to perform too many diverse tasks. It is usually best to break such a function into several smaller functions.

General Terminologies

  • We invoke a function by a function call.
  • Calling function – that invokes a function
  • Called function – that is being invoked
  • When a function completes its task, it either returns a result or simply returns control to the caller

Generic Syntax of a function definition

Following is a generic syntax of a function in C++

ReturnType  functionName (agrumentList) {
    Function Body


void drawLine();      // function prototypes
void printHello();    //     ...
void printGoodbye();  //     ...

int main(void)
  drawLine();        // function call for drawLine
  printHello();      // function call for print the Hello
  printGoodbye();    // function call for print the Goodbye
  drawLine();        // draw the 2nd line on screen
void drawLine()      //Function Definition
  for (int i=1;i<=20; i++) {

Function Prototype

Just like other C++ identifiers, a function must be declared before it can be referenced/call. One way is to insert a function prototype before the main function. The data type of a function is determined by the type of value it returns.

Function Definitions

A function prototype only specifies the number of arguments a function accepts and the type of its result. Where are the operations of a functions? Function definitions are needed for each function subprogram similar to the definition of main function. Every function has two parts

  • Function heading
  • Function body

Function heading is similar to the function prototype except that it does not end with a semicolon. Function body, enclosed in braces, consists step by step instructions/operations for the solution of a given problem. If the function has return type void, we can omit writing return statement in a function.

Each function may contain declaration of its own variables. These variables are called local to that function. In other words, they can be referenced only within that function.

Placement of Functions in a Program

Function prototypes precedes the main function i.e., after the #include directives. Function definitions follow the main function. The relative order of the function definition does not affect their order of execution. Order of execution is determined by the function call

The compiler processes the function prototype before it translates the main function. The information in each prototype is sufficient to correctly translate a call to that function. The compiler translates a function call statement as a transfer of control to function. Memory is allocated for all the variables of the function, whenever a function is called. After the control is transferred back, memory is released and all variables are destroyed


Tagged with: C/C++ language

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.