Programming in C Functions 7
Programming in C Functions 7
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 thingsa function that calls or activates
the function and the function itself.
message( )
{
printf ( "\nSmile, and the world smiles with you..." ) ;
}
int main( )
{
message( ) ;
printf ( "\nCry, and you stop the monotony!" ) ;
return 0;
}
itlay( )
{
printf ( "\nI am in itlay" ) ;
}
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( ).
After each function has done its thing, control returns to main( ).When
main( ) runs out of function calls, the program ends.
School of Computer Engineering
Function Example
6
brazil( )
{ int main( )
printf ( "\nI am in brazil " ) ; {
argentina( ); printf ( "\nI am in main" ) ;
printf ( "\nI am back in brazil" ) ; itlay( ) ;
} printf ( "\nI am back in main" ) ;
return 0;
argentina( ) }
{
printf ( "\nI am in argentina" ) ;
}
itlay( )
{
printf ( "\nI am in itlay" ) ;
brazil( );
printf ( "\nI am back in itlay" ) ;
}
School of Computer Engineering
Summarize what we have learnt so far
7
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
message2( )
{
printf ( "\nBut the butter was bitter" ) ;
}
message1( )
{
printf ( "\nMary bought some butter" ) ;
}
int main( )
{
message1( ) ;
message2( ) ;
return 0;
}
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.
A function can call itself. Such a process is called recursion. We would discuss this
aspect of C functions later in this chapter.
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.
A function can be called from other function, but a function cannot be defined in
another function.
function_name :
This is the name given to the function
it follows the same naming rules as that for any valid variable in C.
return_data_type:
This specifies the type of data given back to the calling construct by the function
after it executes its specific task.
int fun( )
{
char ch ;
If we want that a called function should not return any value, in that case, we must
mention so by using the keyword void as shown below.
void display( )
{
printf ( "\nHeads I win..." ) ;
printf ( "\nTails you lose" ) ;
}
A function can return only one value at a time. Thus, the following statements are
invalid.
return ( a, b ) ;
return ( x, 12 ) ;
C language follows the second order. Consider the following function call:
fun (a, b, c, d ) ; In this call it doesnt matter whether the arguments are passed
from left to right or from right to left.
int i = 10, j = 20 ;
printf ( "%d %d %d ", i, j ) ;
printf ( "%d", i, j ) ;
Any C function by default returns an int value. More specifically, whenever a call is made
to a function, the compiler assumes that this function would return a value of the type
int. If we desire that a function should return a value other than an int, then it is
necessary to explicitly mention so in the calling function as well as in the called function.
Suppose we want to find out square of a number using a function. This is how this simple
program would look like:
square ( float x ) And here are three sample runs of this program...
{
float y ; Enter any number 3
y=x*x; Square of 3 is 9.000000
return y;
} Enter any number 2.5
int main( ) Square of 2.5 is 6.000000
{
float a, b ; How to resolve?
printf ( "\nEnter any number " ) ;
Define the return type of square as float and
scanf ( "%f", &a ) ;
declaring a function prototype as
b = square ( a ) ;
float square ( float ) ;
printf ( "\nSquare of %f is %f", a, b ) ;
at the beginning of the program
return 0;
} School of Computer Engineering
Function Prototype
22
Provides the compiler with the description of functions that will be used later in the
program
Class Work
Write a function to add 3 numbers
Write a function to compute nk where n and k are supplied by user
Write a function to reverse a number
Assume
that the
number
entered
through
scanf( )
is 3
Let us now see how to pass an entire array to a function rather than its individual
elements.
Approach 1 Approach 2
void display(int *, int); /* Function Prototype */ display(int [], int); /* Function Prototype */
int main( ) int main( )
{ {
int num[ ] = { 24, 34, 12, 44, 56, 17 } ; int num[ ] = { 24, 34, 12, 44, 56, 17 } ;
display (&num[0], 6 ) ; display(num, 6) ;
return 0; return 0;
} }
display ( int *q, int row, int col ) display ( int q[][4], int row, int col )
{ {
int i, j ; int i, j ;
for ( i = 0 ; i < row ; i++ ) for ( i = 0 ; i < row ; i++ )
{ {
for ( j = 0 ; j < col ; j++ ) for ( j = 0 ; j < col ; j++ )
{ {
printf ( "%d ", * ( q + i * col + j ) ) ; printf ( "%d ", q[i][j]) ;
} }
printf ( "\n" ) ; printf ( "\n" ) ;
} }
} }
School of Computer Engineering
34
Thank You