Hiding Details
Functions can also be used to hide details. What is the point ?
- To make our main program simpler and easier to read. For instance, we
could call a function named PrintGreeting() and assume that it
will do just that. We have now managed to remove many lines of
code that just clutter the main program.
Here is an example:
/*PrintGreeting prints the greeting for */
/*the program. */
/*Inputs: none Outputs: none (prints to screen)*/
void PrintGreeting( void )
{
printf("\n\n");
printf("*************************************\n");
printf("*Hello. *\n");
printf("*This program finds the surface area*\n");
printf("*and volume of a sphere. *\n");
printf("*************************************\n\n");
}
- In order for other programmers to make use of functions we've written,
it is sufficient for them to know what the function does, what
arguments it takes and its return type. They do not need to know how
we decided to write the code within the function definition.