Function Calling in C Programming Language

Rumman Ansari   Software Engineer   2022-10-12   13611 Share
☰ Table of Contents

Table of Content:


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.

Program

 
#include <stdio.h>
void main( )
{
	display() ;  // function calling
	printf ( "\nCry, and you stop the monotony!" ) ;
}

void display()
{
	printf ( "\nSmile, and the world smiles with you..." ) ;
}
 
function calling in c

Output

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 display( ). What do we mean when we say that main( ) 'calls' the function display( )? We mean that the control passes to the function display( ). The activity of main( ) is temporarily suspended; it falls asleep while the display( ) function wakes up and goes to work. When the display( ) 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 display( ) 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:

Program

  
#include <stdio.h>
void main( )
{
printf ( "\nI am in main" ) ;
	india( ) ;
	bangladesh( ) ;
	australia( ) ;
}

void india( )
{
	printf("\nI am in india") ;
}
void bangladesh( )
{
	printf("\nI am in bangladesh") ;
}
void australia( )
{
	printf("\nI am in australia") ;
}
 

Output


I am in main
I am in india
I am in bangladesh
I am in australia 

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.


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.

Program

  
#include <stdio.h> 
void main( )
{
	printf("\nI am in main") ;
	india( ) ;
	printf("\nI am finally back in main") ;
}

void india( )
{
	printf("\nI am in india") ;
	bangladesh( ) ;
	printf("\nI am back in india") ;
}

void bangladesh()
{
	printf("\nI am in bangladesh") ;
	australia( ) ;
}

void australia()
{
	printf("\nI am in australia") ;
}
 

Output


I am in main
I am in india
I am in Bangladesh
I am in Australia
I am back in India
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.