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); int main(int argc, char* argv[]) { int Index; 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) { 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; }
Move on to More Functions
Comments, suggestions, ideas to
Stuart Banner
