
Function Calling in C: Usage and Examples
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..." ) ; }

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()
.Whenmain()
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.
- Question 1: What is function prototype?
Related Questions
- Assignment 1: Function in C Programming Language
- Assignment 2: Write a program that uses a function to check whether a given year is a leap year or not.
- Assignment 3: Write C a program that uses a function to convert a temperature from Fahrenheit scale to Celsius scale.
- Assignment 4: Write C a program that uses a function show call by value mechanism.
- Assignment 5: Write a function that uses a function to find the greatest common divisor (GCD) of two integers. Write C a program to test the function.
- Assignment 6: Write C a program to print the prime factors of a given number using a function.
- Assignment 7: Write C a program to show that when arrays or strings are passed to a function, call by value mechanism is not followed.
- Assignment 8: Write a C program that uses a function to sort an array of integers using bubble sort algorithm.
- Assignment 9: Write a program in C that uses a function to find the average age of students of a class chosen for a junior quiz competition.
- Assignment 10: Write a program that uses a function to find the maximum value in an array.
- Assignment 11: Write a program that uses a function to copy one string into another without using the strcpy() function available in the standard library of C.
- Assignment 12: Write a program that uses a function to perform addition and subtraction of two matrices containing integer numbers.
- Assignment 13: write a program that uses a function to swap values stored in two integer variables to understand the concept of local variables.
- Assignment 14: write a program that uses a function to swap values stored in two integer variables to understand the concept of global variables.
- Assignment 15: Write a C program that uses a recursive function for generating the Fibonacci numbers.