9.1 – Functions getting started

by subbu on November 12, 2013

Functions in C language

Functions is an interesting and important part of programming. You can’t be a good programmer without having knowledge in functions. Step by step approach with demonstrative programs helps to learn functions more easily. Reading few pages, working on some problems with patience makes you master in functions. Believe me; once you know functions then it would be so easy to learn any programming language because the languages like c++, Java or dot Net are with the full of functions.

What is function?

A word with a pair of parenthesis is called a function. Some of the statements like clrscr(), getch(), exit(), printf() and scanf() are the words with parenthesis, so called functions. Even the main() is a word with parenthesis, so it is also called as a function

main()   /* main is a function */
{

}

We can define our own function with a valid identifier (name) and a parenthesis. A statement or statements are defined with a pair of braces is called function body.

hello()
{
  printf("Hello world");
  printf("\nCoding fox");
}

Here “hello” is the name of function and the statements in a pair of braces is the body. All together it is called the function definition.

Now you may have a doubt that, if(), switch(), for() and while() are the words with parenthesis, are these functions? No, these are not functions because these are keywords with a parenthesis but not with the words (identifiers).

main() and sub functions:

The entry point and exit point of any C program is the main() function. A C program may have any number of functions but, must have at least one that is the main() function. Functions other than the main() function are called sub functions. Unlimited number of functions can be defined in a C program but, any two functions in a C program must not have the same name.

#include<stdio.h>
main()                           /* entry point */
{
   printf("Hello world");
}                                /* exit point   */
fox()
{
   printf("\nCoding Fox");
}
duck()
{
   printf("\nDuck First");
}

Output:
Hello world

The above C program has three functions those are main(), fox() and duck() but, none of the sub functions are executed because execution start from the main() and exit from the main().

To involve the sub functions in execution, we need to call them from the main(). The function calling statement must have the function name with a pair of parenthesis and terminated with a semicolon.

#include<stdio.h>
main()                           /* entry point    */
{
  printf("Hello world");
  fox();                        /* calling fox()   */
  duck();                       /* calling duck()*/
}                               /* exit point      */
fox()
{
  printf("\nCoding Fox");
}
duck()
{
  printf("\nDuck First");
}

Output:
Hello world
Coding fox
Duck First

Example explained:
function callingThe function calling statement fox(); sends the control to the function fox(), returns back to the main() after the completion of function execution.
The function calling statement duck(); send the control to the sub function duck(), returns back to the main() on completion of function execution.

Order of calling is important not the order of definition:

Sub functions can be defined in any order; the order of execution depends on the order of calling sub functions.

#include<stdio.h>
main()                           /* entry point    */
{
  printf("Hello world");
  fox();                        /* calling fox()   */
  duck();                       /* calling duck()*/
}                               /* exit point      */
duck()
{
  printf("\nDuck First");
}
fox()
{
  printf("\nCoding Fox");
}

Output:
Hello world
Coding fox
Duck First

Here the function fox() is defined after the duck() but, called in the same order as in the previous example. Hence, produced the same output

A sub function can call another sub function

In a C program with multiple functions no function is boss, no need of calling every function from the main(). A sub function can call another sub function, even a sub function can call the main() function.

#include<stdio.h>
main()
{
   printf("Hello world");
   fox();                            /* calling fox()     */
}
fox()
{
   printf("\nCoding Fox");
   duck();                           /* calling duck()  */
   printf("\nTesting Fox");
}
duck()
{
   printf("\nDuck First");
}

Output:
Hello world
Coding Fox
Duck First
Testing Fox

Example explained:
nested function callingHere the function fox() is called by the main function. Were as the function duck() is called by the sub function fox()

A function can be called for any number of times

It takes time to shift control from the calling function to the called by function and back to the calling function after the completion of function execution. A function can be called for any number of times but, reduces the performance of program.

#include<stdio.h>
main()                     /* Calling function */
{
  fox();
  fox();
}
fox()                        /* Called by function */
{
  printf("\nCoding Fox");
}

Output:
Coding Fox
Coding Fox


#include<stdio.h>
main()
{
  int i;
  for(i=1;i<=5;i++)
      fox();            /* calls the function for 5 times */
}
fox()
{
  printf("\nCoding Fox");
}

Output:
Coding Fox
Coding Fox
Coding Fox
Coding Fox
Coding Fox

Previous post:

Next post: