Amazon Honor System Click Here to Pay Learn More

Chapter 2
by Forest J. Handford

This chapter covers recursion, functions, and do-while loops

In this chapter we will make two programs to factor numbers. This is a common program that teachers give and also the type of program on some game programming quizzes.

C and C++ both use functions the same way. A function being sets of code that can be executed by other functions in the same program. The main function is a special type of function in that it is always executed when a program is started. The main function can then call other functions. These functions can be part of the library of functions that come with your compiler or you can write them yourself. A function is always declared as follows:

type Name(arguments)
{
   return (type);
}

The type is returned to the calling program. If the function is the main function then the operating system is the calling program. If the function type is void you don't have to return anything. The function stops executing as soon as it reaches a return statement or the function's closing brace. When we call functions like printf we pass it arguments. These arguments are what get passed to the function. Once inside of a function the code can only access the arguments passed in as well as new variables.

The main function can also be passed arguments. The arguments passed to main are used to access any characters that were typed after the name of the routine. This tutorial will not go over how to use the arguments for main. To learn more about it you can purchase........

To allow the use of arguments you must declare what type they are and then give them a name. If the calling function passes a different type then the value will be converted. Some compilers will not let you call a function with arguments of types differing from the arguments in the function’s declaration.

If the function is not above the function that calls it in the file it's in you will have to use a header file to declare it with a function prototype or you will have to place a function prototype above the calling function. Consider the following function prototype:

int Silly(int Number, char Letter);

The above prototype informs the compiler that there will be a function later in the file or in a separate file that is named Silly and that Silly can take an integer as the first argument and a character as the second argument and that it will return an integer. Here are two examples of how you could call the Silly function:

int Value = Silly(1, 'A');
int Number = 20;
char Alpha = 'l';
int Value;
Value = Silly(Number, Alpha);

You can initialize multiple variables of the same type as follows:

int Number, Count, Value;

All three of the above variables will be integers.

The if statement may be the most important statement in C. Consider the following:

if(Number < 2)
{
   Number += 2;
}

The above code checks if Number is less then 2. If Number is less than too it does all the code between the braces. The += operator does the same as the following code:

Number = Number + 2;

This allows programmers to only write a variable once and save time. Similar operators are -=, /=, *=, and %= .

In conjunction with the if statement you can use the else statement as follows:

if(Number < 2)
{
   Number += 2;
}
else
{
   Number -= 2;
}

The above code will check if Number is less than two. If Number is less than two it will add two to number. If number is not less than two it will subtract two from number. Now we should look at the else if statement:

if(Number < 2)
{
   Number += 2;
}
else if(Number == 2)
{
   printf("The number is equal to two!");
}
else if(Number == 3)
{
   printf("The number is equal to three!");
}
else
{
   Number -= 2;
}

The above code will check if number is less than two. If number is less than two, it will add two to Number and ignore the rest of the code. If number is not less than two it will check if number is equal to two. If number is equal to two it will print to the screen "The number is equal to two!" and skip the rest of the code. If number is equal to three the code will print "The number is equal to three!" and skip the rest of the code. If the code reaches the else statement because the if and else if statements evaluated to be false 2 will be subtracted from Number.

In the first of the two programs for this chapter I used a do while loop. do while loops are like while loops except that the condition is evaluated after the code. Consider the following from our next program:

do
{
   Remainder = InputTest % Diviser;

   if(Remainder == 0)
   {
      if(LoopTest == 0)
      {
         LoopTest = 1;
         printf("%i = ", InputTestCopy);
      }
      printf("%i * ", Diviser);
      InputTest /= Diviser;
   }
   else
      if (Diviser == 2) Diviser++;
      else Diviser += 2;
}
while(Diviser < InputTest);

In the above code do is at the beginning of the loop and then while is at the end. A way to have code leave a loop is to use the break command. The break command will take the program to the end of the inner most loop that it is currently in.

We are going to need to learn about the ++ and the -- operator. They are unique operators that can come in very handy. If ++ is written after a variable then the variable becomes 1 more than it's previous value. If -- is written after a variable the variable becomes 1 less than it's previous value. The operators can also come before the variable, but that changes what they do. Consider the following code:

int Var = 3;
Var--;

Var will equal 2 after the second statement.  The developers of C are fans of fancy code.  Look at the following code:

int Count = 0;

while(Count < StringSize)
{
    Array[Count++] = Count;
}

The above code in the loop is equivalent to the following:
Array[Count] = Count;
Count = Count + 1;

The ++ and -- operator when preceding the variable has the highest precedence in the order of operations.  The ++ and -- operator following the variable has the lowest precedence in the order of operations and is executed after all other operations including the assignment operation!

Now we are ready for our first program. I originally wrote this program as an answer to a quiz given by Tiburon for prospective employees:

[ Code and Program ]

The big difference between the above program and the following program is that the above uses iteration and the below uses recursion. Iteration is the use of loops. Recursion is a function that calls itself. It can be tough to decide whether to use recursion or iteration. I usually use iteration to avoid confusing myself, which is not very hard.

Here is the recursive version of a factorial program:

[ Code and Program ]

That is all for this chapter.

PREVIOUS CHAPTER       HOME      NEXT CHAPTER

Amazon Honor System Click Here to Pay Learn More