|
In modern C programming, it is considered good practice to use prototype declarations for all functions that you call. As we mentioned, these prototypes help to ensure that the compiler can generate correct code for calling the functions, as well as allowing the compiler to catch certain mistakes you might make.
Strictly speaking, however, prototypes are optional. If you call a function for which the compiler has not seen a prototype, the compiler will do the best it can, assuming that you're calling the function correctly.
If prototypes are a good idea, and if we're going to get in the habit of writing function prototype declarations for functions we call that we've written (such as multbytwo), what happens for library functions such as printf? Where are their prototypes? The answer is in that boilerplate line.
we've been including at the top of all of our programs. stdio.h is conceptually a File full of external declarations and other information pertaining to the ``Standard I/O'' library functions, including printf. The #include directive (which we'll meet formally in a later chapter) arranges that all of the declarations within stdio.h are considered by the compiler, rather as if we'd typed them all in ourselves. Somewhere within these declarations is an external function prototype declaration for printf, which satisfies the rule that there should be a prototype for each function we call. (For other standard library functions we call, there will be other ``header Files'' to include.) Finally, one more thing about external function prototype declarations. We've said that the distinction between external declarations and defining instances of normal variables hinges on the presence or absence of the keyword extern. The situation is a little bit different for functions. The ``defining instance'' of a function is the function, including its body (that is, the brace-enclosed list of declarations and statements implementing the function). An external declaration of a function, even without the keyword extern, looks nothing like a function declaration. Therefore, the keyword extern is optional in function prototype declarations. If you wish, you can write
and this is just as good an external function prototype declaration as.
extern int multbytwo(int);
|
(In the first form, without the extern, as soon as the compiler
sees the semicolon, it knows it's not going to see a function body, so the
declaration can't be a definition.) You may want to stay in the habit of
using extern in all external declarations, including function
declarations, since ``extern = external declaration'' is an
easier rule to remember.
|
|
A recursive function is one which calls itself. This is another complicated idea which you are unlikely to meet frequently. We shall provide some examples to illustrate recursive functions.
Recursive functions are useful in evaluating certain types of mathematical function. You may also encounter certain dynamic data Structures such as linked lists or binary trees. Recursion is a very useful way of creating and accessing these Structures.
Here is a recursive version of the factorial function. We saw a non recursive version of this earlier.
|
#include<conio.h>
#include<stdio.h>
void main()
{
int n,fact;
int rec(int);
clrscr();
printf("Enter a Number");
scanf("%d",&n);
fact= rec(n);
printf("%d",fact);
}
int rec(int x)
{
int f;
if (x==1)
return (1);
else
f=x*rec(x-1);
return(f);
}
|
Don't be frightened by the apparent complexity of recursion. Recursive functions are sometimes the simplest answer to a calculation. However there is always an alternative non-recursive solution available too. This will normally involve the use of a loop, and may lack the elegance of the recursive solution.
More about recursion
|