What is a Function in C ?

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

Example of Function in C

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

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

More example of function in C

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

Passing Values between Functions

The mechanism used to convey information to the function is the ‘argument’. You have unknowingly used the arguments in the printf( ) and scanf( ) functions; the format string and the list of variables used inside the parentheses in these functions are arguments. The arguments are sometimes also called ‘parameters

Consider the following program. In this program, in main( ) we receive the values of a, b and c through the keyboard and then output the sum of a, b and c. However, the calculation of sum is done in a different function called calsum( ). If sum is to be calculated in calsum( ) and values of a, b and c are received in main( ), then we must pass on these values to calsum( ), and once calsum( ) calculates the sum we must return it from calsum( ) back to main( )

Example of Sending and receiving values between functions

main( ) 
{ 
 int a, b, c, sum ; 
 printf ( "\nEnter any three numbers " ) ; 
 scanf ( "%d %d %d", &a, &b, &c ) ; 
 sum = calsum ( a, b, c ) ;
printf ( "\nSum = %d", sum ) ; 
} 
calsum ( x, y, z ) 
int x, y, z ; 
{ 
 int d ; 
 d = x + y + z ; 
 return ( d ) ; 
} 

And here is the output…

Enter any three numbers 10 20 30
Sum = 60

There are a number of things to note about this program:

  • In this program, from the function main( ) the values of a, b and c are passed on to the function calsum( ), by making a call to the function calsum( ) and mentioning a, b and c in the parentheses:
    • sum = calsum ( a, b, c ) ;
    • in the calsum( ) function these values get collected in three variables x, y and z:

calsum ( x, y, z )
int x, y, z ;

  • The variables a, b and c are called ‘actual arguments’, whereas the variables x, y and z are called ‘formal arguments’. Any number of arguments can be passed to a function being called. However, the type, order and number of the actual and formal arguments must always be same Instead of using different variable names x, y and z, we could have used the same variable names a, b and c. But the compiler would still treat them as different variables since they are in different functions.
  • There are two methods of declaring the formal arguments. The one that we have used in our program is known as Kernighan and Ritchie (or just K & R) method.

calsum ( x, y, z )
int x, y, z ;
Another method is,
calsum ( int x, int y, int z )

This method is called ANSI method and is more commonly used these days

Leave a Comment