Working with Functions in C: Top-Down Programming

The notion of functions that call functions that in turn call functions, and so on, forms the basis for producing good, structured programs. In the main routine of Program 8.8, the squareRoot function is called several times. All the details concerned with the actual calculation of the square root are contained within the squareRoot function itself, and not within main. Thus, you can write a call to this function before you even write the instructions of the function itself, as long as you specify the arguments that the function takes and the value that it returns.

Later, when proceeding to write the code for the squareRoot function, this same type of top-down programming technique can be applied:You can write a call to the absoluteValue function without concerning yourself at that time with the details of operation of that function. All you need to know is that you can develop a function to take the absolute value of a number.

The same programming technique that makes programs easier to write also makes them easier to read. Thus, the reader of Program 8.8 can easily determine upon exami- nation of the main routine that the program is simply calculating and displaying the square root of three numbers. She need not sift through all of the details of how the square root is actually calculated to glean this information. If she wants to get more involved in the details, she can study the specific code associated with the squareRoot function. Inside that function, the same discussion applies to the absoluteValue func- tion. She does not need to know how the absolute value of a number is calculated to understand the operation of the squareRoot function. Such details are relegated to the absoluteValue function itself, which can be studied if a more detailed knowledge of its operation is desired.

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.

Leave a Reply

Your email address will not be published. Required fields are marked *