A function is the fundamental building block of any program, as functions perform the tasks required in that program. The most important in a C++ program is called main ( ). The main ( ) function is automatically executed when the program is run. In effect, main ( ) is the program. A typical main function looks like this:

Int main (void)

{

// Your code here

return 0;

}

Void NoValueReturned(int x, int y)

  {

  // code here

  }


Writing functions

Consider a function, which calculates the square of a number:

Int Square(int input)

{

return(input * input);

}

This function returns a value of type int, is called Square and requires one input argument, of type int, called input by that function. Inside the function, input refers to whatever int value was passed to the function. As can be seen below, the calling code does not have to call the int it passes to this function input :so long as the callers value is of type int it can be used as an input to Square(). Similarly to a variable, before this function can be used by other code, it must first be declared:

void main (void)

{

int Square( int ); //declare Square ( )

int x, y, result; //declare variables

x = 4 * 2;

y = x / 7;

result = Square ( y ); //call square function with y as input

}