Sams Teach Yourself C in 21 Days

(singke) #1
If the return value of the function meets the criteria (in this case, if half_of()returns a
value greater than 10), the ifstatement is true, and its statements are executed. If the
returned value doesn’t meet the criteria, the statements in the ifare not executed.
The following example is even better:
if ( do_a_process() != OKAY )
{
/* statements; */ /* do error routine */
}
Again, actual statements are not provided, nor is do_a_process()a real function; how-
ever, this is an important example. The return value of a process is checked to see
whether it ran correctly. If it didn’t, the statements take care of any error handling or
cleanup. This is commonly used with accessing information in files, comparing values,
and allocating memory.

116 Day 5

If you try to use a function with a voidreturn type as an expression, the
Caution compiler generates an error message.

DOpass parameters to functions in order
to make the function generic and thus
reusable.
DOtake advantage of the ability to put
functions into expressions.

DON’Tmake an individual statement
confusing by putting a bunch of func-
tions in it. You should put functions into
your statements only if they don’t make
the code more confusing.

DO DON’T


Recursion ......................................................................................................

The term recursionrefers to a situation in which a function calls itself either
directly or indirectly. Indirect recursionoccurs when one function calls another
function that then calls the first function. C allows recursive functions, and they can be
useful in some situations.
For example, recursion can be used to calculate the factorial of a number. The factorial of
a number xis written x!and is calculated as follows:
x! = x * (x-1) * (x-2) * (x-3) * ... * (2) * 1

NEWTERM

09 448201x-CH05 8/13/02 11:15 AM Page 116

Free download pdf