As the problems you work on get larger, it becomes ever more important to break them down into smaller parts. The smaller parts should each deal with a small task. While there are important advantages, there is also a great danger that appears as programs get larger and larger -- it becomes more and more likely that variables will be inadvertently altered. This can have dire effects on the program and can be extremely difficult to debug.
C and many other high-level programming languages provide a means to avoid accidentally changing the values of variables.
If the value of a variable will not be changed inside a function, then a only the value is passed to the function called. If the function gets only the value and not the location where it is stored, the function is unable to change it. This is demonstrated in the function PrintingResults(). It receives the values for the area, base, and height of the triangle. It does not change them -- it only prints them.
On the other hand, if the intent of a function is to get values from the user, the function must know where to store the values. In this case, the addresses of the variables to be filled are sent to the function. This is accomplished by using the & in the function call. Inside the function, these addresses are referenced using pointer parameters. Because scanf() in InputtingData() requires a pointer or address to store the data read from the keyboard, the & is not used to precede the name of the parameter as was done in Functions I, since b and h are themselves pointers, pointing at the locations of the global variables base and height, respectively.
In still other cases, values are needed to calculate a new value which is needed in main() or some other calling function. This is shown with the Calculation() function, where the values stored in the global variables base and height are used to calculate the area. Here the values are passed for the base and height and are accessed inside the function using the parameters named b and h, because they will not be changed. However, the address for the area variable is passed, because the value is needed back in main(). Consequently, *A is used to store the result at the address pointed at by A.
#include <stdio.h> /* standard I/O header file */
/* function prototypes */
void Introduction();
void InputtingData(int *, int *);
void Calculation(int, int, float *);
void PrintingResults(int, int, float);
void main()
{
int base;
int height;
float Area;
Introduction();
InputtingData(&base, &height);
Calculation(base, height, &Area);
PrintingResults(base, height, Area);
}
void Introduction()
{
printf("This program calculates and prints the area of a triangle\n");
printf("after you enter its dimensions.\n");
printf("When asked to, type in a dimension and hit the RETURN key.\n");
printf("\n");
} /* Introduction */
void InputtingData(int *b, int *h)
{
printf("What is the length of the triangle's base? ");
scanf("%d", b);
printf("What is the triangle's height? ");
scanf("%d", h);
} /* InputtingData */
void Calculation(int b, int h, float *A)
{
*A = b * h / 2.0;
} /* Calculation */
void PrintingResults(int b, int h, float A)
{
printf("\n");
printf("The area of a triangle with a base of %d units and\n", b);
printf("a height of %d units is %.1f square units.\n", h, A);
} /* PrintingResults */
© 1998-2001 DFStermole
HTMLified: 4 Oct 98
Modified 01 Dec 01