Functions in C Programming Language
Functions in C Programming Language
1
For example:
2
int mult ( int x, int y );
This prototype specifies that the function mult will
accept two arguments, both integers, and that it
will return an integer. Do not forget the trailing
semi-colon. Without it, the compiler will probably
think that you are trying to write the actual
definition of the function.
#include <stdio.h>
3
int main()
{
int x;
int y;
5
defined before it is used, we could do away with
the prototype--the definition basically acts as a
prototype as well.
6
would probably best be served by making functions
for each of the actual menu choices, and then
breaking down the complex tasks into smaller,
more manageable tasks, which could be in their
own functions. In this way, a program can be
designed that makes sense when read. And has a
structure that is easier to understand quickly. The
worst programs usually only have the required
function, main, and fill it with pages of jumbled
code.
Recursion
Recursion is a programming technique that allows
the programmer to express operations in terms of
themselves. In C++, this takes the form of a
function that calls itself. A useful way to think of
recursive functions is to imagine them as a process
being performed where one of the instructions is to
"repeat the process". This makes it sound very
similar to a loop because it repeats the same code,
and in some ways it is similar to looping. On the
other hand, recursion makes it easier to express
ideas in which the result of the recursive call is
necessary to complete the task. Of course, it must
be possible for the "process" to sometimes be
completed without the recursive call. One simple
example is the idea of building a wall that is ten
feet high; if I want to build a ten foot high wall,
7
then I will first build a 9 foot high wall, and then
add an extra foot of bricks. Conceptually, this is
like saying the "build wall" function takes a height
and if that height is greater than one, first calls
itself to build a lower wall, and then adds one a
foot of bricks.
A simple example of recursion would be:
void recurse()
{
recurse(); /* Function calls itself */
}
int main()
{
recurse(); /* Sets off the recursion */
return 0;
}
This program will not continue forever, however.
The computer keeps function calls on a stack and
once too many are called without ending, the
program will crash. Why not write a program to see
8
how many times the function is called before the
program terminates?
#include <stdio.h>
int main()
{
9
recurse ( 1 ); /* First function call, so it starts at
one */
return 0;
}
This simple program will show the number of times
the recurse function has been called by initializing
each individual function call's count variable one
greater than it was previous by passing in count +
1. Keep in mind that it is not a function call
restarting itself; it is hundreds of function calls that
are each unfinished.
10
have a condition that controls when the function
will finally stop calling itself. The condition where
the function will not call itself is termed the base
case of the function. Basically, it will usually be an
if-statement that checks some variable for a
condition (such as a number being less than zero,
or greater than some other number) and if that
condition is true, it will not allow the function to call
itself again. (Or, it could check if a certain condition
is true and only then allow the function to call
itself).
A quick example:
12
void printnum ( int begin )
{
printf( "%d", begin );
if ( begin < 9 ) /* The base case is when
begin is no longer */
{ /* less than 9 */
printnum ( begin + 1 );
}
/* display begin again after we've already
printed everything from 1 to 9
* and from 9 to begin + 1 */
printf( "%d", begin );
}
This function works because it will go through and
print the numbers begin to 9, and then as each
printnum function terminates it will continue
printing the value of begin in each function from 9
to begin.
13
(number - 1) * (number - 2) ... * 1).
14