Functions are the building blocks of any programming language.

A Function is a self contained block of code with a specific purpose. It has a name that is used to identify and call it for execution. The function name is global, but it is not necessarily unique in C++. Ivor Hoprton.

Declaration of a Function

The standard form of declaration of a function is

Structure of a Function

There are two main parts of the function. The function header and the function body.

In the first line of the above code

It has three main parts

  1. The name of the function i.e. sum
  2. The parameters of the function enclosed in paranthesis
  3. Return value type i.e. int

Function Body

What ever is written with in { } in the above example is the body of the function.

Function Prototypes

The prototype of a function provides the basic information about a function which tells the compiler that the function is used correctly or not. It contains the same information as the function header contains. The prototype of the function in the above example would be like

The only difference between the header and the prototype is the semicolon ;, there must the a semicolon at the end of the prototype.

Function: Return Statement

The return statement in a function has two main purposes.

  1. It can be used to exit the function at any time during the exection of a function. e.g. it returns the control back to the calling code.
  2. It can also be used to return a value.

This function returns the control back to the calling function when the for loop finishes execution.

Mostly function do not use this method toterminate execution. Most of the functions returns a value on specified conditions. Here is an example that will show how to terminate the function upon some condition.

This function returns the indicator after its execution. We can have a function which returns the calculated valuse.