A computer program cannot handle all the tasks by it self. Instead its requests other program
like entities - called ‘functions in C - to get its tasks done. A function is a self contained block of statements that perform a coherent task of some kind.
e.g
message();
{
message ( );
printf (“\n Hello “);
}
main ( )
{
message ( )
printf (“\n I am in main “);
}
output of the program will be
Hello
I am in main
Here main ( ) is the calling function and message is the called function. When the function message ( ) is called the activity of main ( ) is temporarily suspended 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.
The General form of a function is :-
function (arg1, arg2, arg3)
type arg1, arg2, arg3
{
statement 1;
statement2;
statement3;
statement4;
}
There are basically two types of functions
(i) Library functions e.g printf ( ), scanf ( ) etc
(ii) user defined function e.g the function message( ) mentioned above.
The following point must be noted about functions
(i) C program is a collection of one or more functions
(ii) A function gets called when the function name is followed by a semicolon for e.g. main ( )
{
message ( );
}
(iii) A function is defined when function name is followed by a pair of braces in which one or more statements may be present for e.g.
message ( )
{
statement 1;
statement2;
statement 3;
}
(iv) Any function can be called from any other function even main ( ) can be called from other functions. for e.g.
main ( )
{
message ( );
}
message ( )
{
printf (“ \n Hello”);
main ( );
}
(v) A function can be called any number of times for eg. main ()
{
message ( );
message ( );
}
message ( )
{
printf (“\n Hello”);
}
(vi) 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 e.g.
main ( );
{
message1 ( );
message2 ( );
}
message2 ( )
{
printf (“\n I am learning C”);
}
message1 ( )
{
printf ( “\n Hello “);
}
(vii) A function can call itself such a process as called ‘recursion’.
(viii) A function can be called from other function, but a function cannot be defined in an- other function. Thus the following program code would be wrong, since argentina is being defined inside another function main ( ).
main ( )
{
printf (“\n I am in main”);
argentina ( )
{
printf {“\n I am in argentina”);
}
}
(ix) Any C program contains at least one function
(x) If a program contains only one function, it must be main ( )
(xi) In a C program if there are more than one functional present then one of these func- tional must be
main ( ) because program execution always begins with main ( )
(xii) There is no limit on the number of functions that might be present in a C program.
(xiii) Each function in a program is called in the sequence specified by the function calls in
main ( )
(xiv) After each function has done its thing, control returns to the main ( ), when main ( ) runs out of function calls, the program ends.
like entities - called ‘functions in C - to get its tasks done. A function is a self contained block of statements that perform a coherent task of some kind.
e.g
#include"stdio.h"
main ( ) because program execution always begins with main ( )