Predefined Functions and Code Reuse
A primary goal of software engineering is to write error-free
code. Code reuse, reusingprogram fragments that have already been written and tested
whenever possible, is one way to accomplish this goal. Stated more simply, “Why
reinvent the wheel?” C promotes reuse by providing many predefined functions
that can be used to perform mathematical computations. C’s standard math
library defines a function named sqrt that performs the square root
computation. The function call in the assignment statement
y = sqrt(x);
activates the code for function sqrt , passing the argument x to the
function. You activate a function by writing a function call. After the
function executes, the function result is substituted for the function call. If
x is 16.0 , the assignment statement above is evaluated as follows:
1.
x is 16.0 , so function sqrt
computes the 116.0 or 4.0.
2.
The function result, 4.0 , is assigned to y
.
A function
can be thought of as a “black box” that has passed one or more input values and
automatically returns a single output value. Figure 3.6 illustrates this for the
call to function sqrt .
The value of x ( 16.0 ) is the function input, and
the function result, or output, is 116.0 (result is 4.0 ).
If w is 9.0 , the assignment statement
z
= 5.7 + sqrt(w);
is evaluated
as follows:
1. w is 9.0 , so function sqrt computes the square root of 9.0 , or 3.0 .
2. The
values 5.7 and 3.0 are added together.
3. The sum, 8.7 , is stored in z .
Use of Color to Highlight New Constructs
In
Fig. 3.7 , program lines that illustrate new constructs are in color, so that
you can find them easily. We will continue to use color for this purpose in
figures that contain programs.
1. /*
2. * Performs three square
root computations
3. */
4.
5. #include
/* definitions of printf, scanf */
6. #include
7.
8. int
9. main(void)
10. {
11. double first, second,
/* input - two data values */
12. first_sqrt, /* output
- square root of first */
13. second_sqrt, /* output
- square root of second */
14. sum_sqrt; /* output -
square root of sum */
15.
16. /* Get first number
and display its square root. */
17. printf("Enter the
first number> ");
18. scanf("%lf",
&first);
19. first_sqrt =
sqrt(first);
20. printf("The
square root of the first number is %.2f\n", first_sqrt);
21. /* Get second number
and display its square root. */
22. printf("Enter the
second number> ");
23. scanf("%lf",
&second);
24. second_sqrt =
sqrt(second);
25. printf("The
square root of the second number is %.2f\n", second_sqrt);
26.
27. /* Display the square
root of the sum of the two numbers. */
28. sum_sqrt =
sqrt(first + second);
29. printf("The
square root of the sum of the two numbers is %.2f\n",
30. sum_sqrt);
31.
32. return (0);
33. }
Enter the first number> 9.0
The square root of the first number is 3.00
Enter the second number> 16.0
The square root of the second number is 4.00
The square root of the sum of the two numbers is 5.00
Posted by 08.50 and have
0
comments
, Published at