https://postimg.cc/gallery/4pgyyjR
[url=
https://postimg.cc/LYsBmFDK][img]https://i.postimg.cc/LYsBmFDK/img-5-14.jpg[/img][/url]
[url=
https://postimg.cc/FkdyJbwT][img]https://i.postimg.cc/FkdyJbwT/img-5-15.jpg[/img][/url]
---
5-14 LEARNING TO PROGRAM IN C SECTION 544
(2) When dump is applied to data other than char data, the internal representation of such data may be different on different machines, and the printouts could look different. The argument is not the same type as dump ex-pects, and lint will complain about the mismatch.
(3) When dump is applied to an arbitrary numerical address, the program could "bomb out" with hardware errors on some machines (from trying to reference non-existent memory locations), and the usage is certainly not portable. This latter type of programming is only to be used when you are quite sure that a non-portable program is needed for one particular machine.
The lint command will certainly complain, with good reason.
5.5 Recursive Functions
As we have seen, each function receives its own frame when it is entered, and this frame is given back when the function returns control. Thus it is easy for a function to call itself, thus becoming a recursive function. The most familiar example of a recursive function is the factorial func-
tion:
n! = n x (n-1)! if n > 0;
= 1 otherwise.
Here is a program containing a factorial function:
---
CHAPTER 5 FUNCTIONS 5-15
fact.c [5-22]:
/* factl - return n! (n factorial)
*/
#include "
local.h"
long factl(n)
long n;
{
if (n <= 1)
return (1);
else
return (n * factl(n - 1));
}
/* fact - demonstrate factl function
*/
main()
{
long factl();
long result;
result = factl((long)3);
printf("3! = %ld\n", result);
exit(SUCCEED);
}
The function definition line
long factl(n)
says that the function will return a long number, that its name is factl, and that it has one parameter, n. The crucial part of the function body is the ex-pression
factl(n - 1)
which calls the factl function from within itself, passing an argument which is one less than the value passed in the parameter n.
We will now trace the execution of the expression fact1(3) in the main program. The value 3 is placed into an argument frame, and the function factl is called. Its parameter n has the value 3:
<- frame for factl(3)
-------------
n | 3 |
| |
-------------
The function in turn executes the expression
factl(n - 1)
thus placing 2 into another argument frame, and calling factl again.