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

Program structure

The course preferred program structure where declared functions are involved:

...
function declarations (prototypes)
int (main(int argc, char* argv[])
{
   main program including calls to declared and library functions
    return 0();
}

function definitions

This is shown below on the following code:


float MT262sqr(float Number);            function declaration
int main(int argc, char* argv[])
{
int Index;                                              main program
  for (Index = 10; Index < 21; Index = Index +1)
  {
     WriteIntCr(Index);
      WriteFloatPrCr("Our square root is ", MT262sqr(Index));
      WriteFloatPrCr("The library value is ", sqrt(Index));
  }
  getchar();
        return 0;
}
//---------------------------------------------------------------------------
float MT262sqr(float Number)               function definitions
{
float Left;
float Right;
float Mid;
  if (Number >= 0)
  {
      Left = 0;
      Right = Number + 1;
      while ((Right - Left) > 0.001)
      {
        Mid = (Left + Right)/2;
        if (Mid*Mid < Number)
          Left = Mid;
        else
          Right = Mid;
      };
      Mid = (Left + Right)/2;
      return Mid;
  }
  else
    return -1;

}
Home| OU Study Rooms | MT262 Index | Block 2 - Structures
Move on to More Functions

Valid CSS! Valid XHTML 1.0!

Comments, suggestions, ideas to
Stuart Banner