Functions and Pointers 2
As we have noted earlier the program execution always begins with main( ). Except for this fact all C functions enjoy a state of perfect equality. No precedence, no priorities, nobody is nobody’s boss. One function can call another function it has already called but has in the meantime left temporarily in order to call a third function which will sometime later call the function that has called it, if you understand what I mean. No? Well, let’s illustrate with an example.
main( )
{
printf ( "\nI am in main" ) ;
italy( ) ;
printf ( "\nI am finally back in main" ) ;
}
italy( )
{
printf ( "\nI am in italy" ) ;
brazil( ) ;
printf ( "\nI am back in italy" ) ;
}
brazil( )
{
printf ( "\nI am in brazil" ) ;
argentina( ) ;
}
argentina( )
{
printf ( "\nI am in argentina" ) ;
}
And the output would look like...
I am in main
I am in italy
I am in brazil
I am in argentina
I am back in italy
I am finally back in main
Here, main( ) calls other functions, which in turn call still other functions. Trace carefully the way control passes from one function to another. Since the compiler always begins the program execution with main( ), every function in a program must be called directly or indirectly by main( ). In other words, the main( ) function drives other functions.
Let us now summarize what we have learnt so far.
(a)
(b)
(c)
C program is a collection of one or more functions.
A function gets called when the function name is followed by a semicolon. For example,
main( )
{
argentina( ) ;
}
A function is defined when function name is followed by a pair of braces in which one or more statements may be
present. For example,
argentina( )
{
statement 1 ;
statement 2 ;
statement 3 ;
}
(d)
(e)
(f)
Any function can be called from any other function. Even main( ) can be called from other functions. For example,
main( )
{
message( ) ;
}
message( )
{
printf ( "\nCan't imagine life without C" ) ;
main( ) ;
}
A function can be called any number of times. For example,
main( )
{
message( );
message( ) ;
}
message( )
{
printf ( "\nJewel Thief!!" ) ;
}
The order in which the functions are defined in a program and the order in which they get called need not necessarily be same. For example,
main( )
{
message1( );
message2( ) ;
}
message2( )
{
printf ( "\nBut the butter was bitter" ) ;
message1( )
{
printf ( "\nMary bought some butter" ) ;
}
Here, even though message1( ) is getting called before message2( ), still, message1( ) has been defined after message2( ). However, it is advisable to define the functions in the same order in which they are called. This makes the program easier to understand.
(g)
(h)
(i)
A function can call itself. Such a process is called ‘recursion’. We would discuss this aspect of C functions later in this chapter.
A function can be called from other function, but a function cannot be defined in another function. Thus, the following program code would be wrong, since argentina( ) is being defined inside another function, main( ).
No comments:
Post a Comment