Functions in C

Functions C

Human is an intelligent species, but still cannot perform all of life’s tasks all alone. He has to rely on others. You may call a mechanic to fix up your bike, hire a gardener to mow your lawn, or rely on a store to supply you groceries every month.

A computer program (except for the simplest one) finds itself in a similar situation. It cannot handle all the tasks by itself. Instead, it requests other program like entities—called “functions” in C—to get its tasks done. In this article we will focus on these functions. We will look at a variety of features of these functions, starting with the simplest one and then working towards those that demonstrate the power of C functions.

What is a Function

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’s time, 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 message( ) 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 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.

For any query or issue, feel free to discuss on http://discuss.eduguru.in