Sunday, October 30, 2011

What is a Function of : C programing The Loop Control Structure

A function is a self-contained block of statements that perform a coherent task of some kind. Every C program can be thought of as a collection of these functions. As we noted earlier, using a function is something like hiring a person to do a specific job for you. Sometimes the interaction with this person is very simple;
sometimes it’s complex.

Suppose you have a task that is always performed exactly in the same way—say a bimonthly servicing of your motorbike. When you want it to be done, you go to the service station and say, “It’stime, do it now”. You don’t need to give instructions, because the mechanic knows his job. You don’t need to be told when the job is done. You assume the bike would be serviced in the usual way, the mechanic does it and that’s that.
Let us now look at a simple C function that operates in much the same way as the mechanic. Actually, we will be looking at two things—a function that calls or activates the function and the function itself.
main( )
{
      message( ) ;
      printf ( "\nCry, and you stop the monotony!" ) ;
}
message( )
{
      printf ( "\nSmile, and the world smiles with you..." ) ;
}

And here’s the output...

Smile, and the world smiles with you...
Cry, and you stop the monotony!

Here, main( ) itself is a function and through it we are calling the function message( ). What do we mean when we say that main( ) ‘calls’ the function message( )? We mean that the control passes to the function message( ). The activity of main( ) is temporarily suspended; it falls asleep while the message( ) function wakes up and goes to work. When the message( ) function runs out of statements to execute, the control returns to main( ), which comes to life again and begins executing its code at the exact point where it left off. Thus, main( ) becomes the ‘calling’ function, whereas

main( )
{
      message( ) ;
      printf ( "\nCry, and you stop the monotony!" ) ;
}
message( )
{
      printf ( "\nSmile, and the world smiles with you..." ) ;
}

And here’s the output...

Smile, and the world smiles with you...
Cry, and you stop the monotony!

Here, main( ) itself is a function and through it we are calling the function message( ). What do we mean when we say that main( ) ‘calls’ the function message( )? We mean that the control passes to the function message( ). The activity of main( ) is temporarily suspended; it falls asleep while the message( ) function wakes up and goes to work. When the message( ) function runs out of statements to execute, the control returns to main( ), which comes to life again and begins executing its code at the exact point where it left off. Thus, main( ) becomes the ‘calling’ function, whereasmessage( ) becomes the ‘called’ function
.
If you have grasped the concept of ‘calling a function you are prepared for a call to more than one function. Consider the following example:

main( )
{
      printf ( "\nI am in main" ) ;
      italy( ) ;
      brazil( ) ;
      argentina( ) ;


becomes the ‘called’ function.
If you have grasped the concept of ‘calling’ a function you are prepared for a call to more than one function. Consider the following example:

main( )
{
      printf ( "\nI am in main" ) ;
      italy( ) ;
      brazil( ) ;
      argentina( ) ; 
}
italy( ) 
{
      printf ( "\nI am in italy" ) ;

}
brazil( ) 
{
      printf ( "\nI am in brazil" ) ; 
}
argentina( ) 
{
      printf ( "\nI am in argentina" ) ;
}

The output of the above program when executed would be as
under:

I am in main
I am in italy 
I am in brazil 
I am in argentina 

From this program a number of conclusions can be drawn:

−  Any C program contains at least one function.

−  If a program contains only one function, it must be main( ).

−  If a C program contains more than one function, then one (and only one) of these functions must be
main( ), because program execution always begins with main( ).

−  There is no limit on the number of functions that might be present in a C program.

−  Each function in a program is called in the sequence specified by the function calls in main( )

−  After each function has done its thing, control returns to main( ).When main( ) runs out of function calls, the program ends.

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 calledit, 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)  C program is a collection of one or more functions.

(b)  A function gets called when the function name is followed by a semicolon. For example,

 main( )
{
     argentina( ) ;
}

(c)  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)  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( ) ; 

}

(e)  A function can be called any number of times. For example,

 main( ) 
{
     message( ) ;
     message( ) ;
}
message( )
{
     printf ( "\nJewel Thief!!" ) ;
}

(f)  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)  A function can call itself. Such a process is called ‘recursion’. We would discuss this aspect of C functions later in this chapter. 

(h)  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( ).

main( )
{
     printf ( "\nI am in main" ) ;
 argentina( )
 {
           printf ( "\nI am in argentina" ) ;
 }
}

(i)  There are basically two types of functions:

 Library functions Ex. printf( ), scanf( ) etc. User-defined functions Ex. argentina( ), brazil( ) etc.

 As the name suggests, library functions are nothing but commonly required functions grouped together and stored in what is called a Library. This library of functions is present on the disk and is written for us by people who write compilers for us. Almost always a compiler comes with a library of standard functions. The procedure of calling both types of functions is exactly same.

No comments:

Post a Comment