MT 262 - Block 2 - Unit 2 - Functions
fellstrider.com - the logo!
Home| OU Study Rooms | MT262 Index | Block 2 - Structures
 
Functions

Separating tasks at the design stage is the modularisation of design. Each part of the design will be a module with its own design and code. Communication between modules is through functions. A user module passes data to a second (function) module. The function processes the data and returns the processed data to the user module. The user wants to know what the function does but not how.

Developing modular designs involves two aspects of each module:

Functions are permitted to have side-efects. One such is the appearance of the input prompt from the ReadIntPr function. However, not all side-effects are harmless. Each item of input data is an argument. The resulting output is the result or value.

Values passed as arguments to functions are the parameters of the function. Programmers need only know what the function does but not the algorithms used to create the processing.

A piece of code to be referred to as MT262sqr
#include 
#include "MT262io.h"
#include 
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
float Left;
float Right;
float Mid;
float Number;
bool NumberOK;
   Number = ReadFloatPr("Enter a non-negative number whose root is required: ");

   if (Number < 0)
   NumberOK = false;
      else
{
NumberOK = true;
Left = 0;
Right = Number + 1;
while ((Right - Left) > 0.001)
   {
      Mid = (Right + Left)/2;
      if (Mid * Mid < Number)
         Left = Mid;
      else
         Right = Mid;
   }
   Mid = (Right + Left)/2;
}
   if (NumberOK)
    {
      WriteFloatPrCr("Our square root is ", Mid);
      WriteFloatPr("The library value is ", sqrt(Number));
    }
   else
      WriteString("The number was negative!");
   getchar();
        return 0;
}

declaring a function is done by:

float MT262sqr (float Number);

The key to the compiler recognising this as a function is the brackets. This instructs the compiler about what the identifier represents and how it should be used. The identifier is MT262sqr. The parameter are inside the brackets (float, Number). (The variable Number is termed the formal parameter, whereas the parameter value sent to the function is termed the actual parameter) The start of the term in this case is float, this denotes the type of result to be returned.

Functions can have any number of parameters or be declared as void, where the parameter list is empty.

Completing the MT262sqr function body

float MT262sqr (float Number)  //declaration
{                                           //start of body
float Left;
float Right;
float Mid;

  if (Number >= 0)
  {
    Left = 0;
    Right = Number + 1;
    while ((Right - Left) > 0.001)
    {
      Mid = (Right + Left)/2;
      if (Mid * Mid < Number)
         Left = Mid;
      else
         Right = Mid;
    }
    Mid = (Right + Left)/2;
    return Mid;		return of the answer is by the keyword return
   }
  else
    return -1;
}                                           //end of body

Function specification

A programmer needs to know:

The specification of the MT262sqr function is given below:

MT262sqr
Real Number [The number whose root is required]
If Number is positive or zero, returns its square root correct to 3 decimal places. Otherwise, the value -1 is returned.
Real MT262sqr (Number) [The square root of Number, or -1.]
Home| OU Study Rooms | MT262 Index | Block 2 - Structures
Move on to Implementing Functions

Valid CSS! Valid XHTML 1.0!

Comments, suggestions, ideas to
Stuart Banner