Now for a refinement on a general rule stated earlier. When you have variables brought to a function as arguments to the function, they are defined immediately after the function name and prior to the opening brace for the program. Other variables used in the function are defined at the beginning of the function, immediately following the opening brace of the function, and before any executable statements.
Standard Function Libraries
Every compiler comes with some standard predefined functions which are available for your use. These are mostly input/ouput functions, character and string manipulation functions, and math functions. We will cover most of these in subsequent chapters.
In addition, most compilers have additional functions predefined that are not standard but allow the programmer to get the most out of his particular computer. In the case of the IBM-PC and compatibles, most of these functions allow the programmer to use the BIOS services available in the operating system, or to write directly to the video monitor or to any place in memory. The Applix equivalents are included in the header files mentioned elsewhere. These will not be covered in any detail as you will be able to study the unique aspects of your compiler on your own. Many of these kinds of functions are used in the IBM versions of the example programs in chapter 14.
What Is Recursion?
Recursion is another of those programming techniques that seem very intimidating the first time you come across it, but if you will load and display the example program named recurson.c, we will take all of the mystery out of it. This is probably the simplest recursive program that it is possible to write and it is therefore a stupid program in actual practice, but for purposes of illustration, it is excellent.
main( )
{
int index;
index = 8;
count_dn(index);
}
count_dn(count)
int count;
{
count--;
printf("The value of the count is %d\n",count);
if (count > 0)
count_dn(count);
printf("Now the count is %d\n",count);
}
Recursion is nothing more than a function that calls itself. It is therefore in a loop which must have a way of terminating. In the program on your monitor, the variable "index" is set to 8, and is used as the argument to the function "count_dn". The function simply decrements the variable, prints it out in a message, and if the variable is not zero, it calls itself, where it decrements it
again, prints it, etc. etc. etc. Finally, the variable will reach zero, and the function will not call itself again. Instead, it will return to the prior time it called itself, and return again, until finally it will return to the main program and will return to DOS.
For purposes of understanding you can think of it as having 8 copies of the function "count_dn" available and it simply called all of them one at a time, keeping track of which copy it was in at any given time. That is not what actually happened, but it is a reasonable illustration for you to begin understanding what it was really doing.
Google Search
Custom Search