Introduction to functions

Introduction to Functions

A function is a self contained program block that carries out some specific and well defined task. Every C program consists of one or more functions. One of these functions must be main().

C programs generally consist of many small functions rather than a few big ones. We will learn in this chapter how C has been designed to make functions efficient and easy to use.

There are two types of functions in C

  1. Library function
  2. User defined function

Benefits of using functions

  • Function is used to break large tasks into smaller one.
  • Function is used to avoid writing same code again and again.

Take a example suppose You wrote a code to print “Welcome to gyantoday.com” or do a specific work. If later in the program you want to print same thing or perform same specific task then you will have to write same code again and again. Honestly no one like to do repeatedly work for same task. This types of work can be avoid by writing function for that task and whenever we need to perform that specific task, we can call that function which has written already.

Lets write a C program with name gt_func1.c

  1.   #include<stdio.h>
  2.   int main( )
  3.   {
  4.       gt_func1( ); /* Function call */
  5.        return 0;
  6.   }
  7.   void gt_func1( )
  8.   {
  9.       printf(“Welcome to gyantoday.com\n”);
  10.  }

If we compile this program. It will give compilation error, to remove compilation error we need to write function prototype. We will learn about function prototype in next topic.

Now see below program. Here i added function prototype, it will compile successfully.

  1.   #include<stdio.h>
  2.   void gt_func1( );   /* Function prototype */
  3.   int main( )
  4.   {
  5.       gt_func1( ); /* Function call */
  6.       printf(” Returning from function gt_func1 \n”);
  7.        return 0;
  8.   }
  9.   void gt_func1( )
  10.   {
  11.       printf(“Welcome to gyantoday.com\n”);
  12.  }

Explanation:-

  1. Here main() function is calling function and function gt_func1() is called function.
  2. See in above program, the statement at line number 5. This statement is a function call, which call a function name gt_func1( ) when compiler read this line, it will jump at line number 9.
  3. After jumping to line number 9 , compiler execute this function line by line. There is only one executable line inside function gt_func1( )  after executing  this function , compiler comes out and returns at line number 5.
  4. After returning from  function gt_func1() to line number 5. Compiler starts execute next line in the calling function.

Lets write another C program with name gt_func2.c

  1. #include<stdio.h>
  2. void gt_func1();  /* Function prototype */
  3. int gt_func2(int x, int y); /* Function prototype */
  4. int main()
  5. {
  6.     int num1 = 10;
  7.     int num2 = 40;
  8.     int sum  = 0;
  9.     gt_func1(); /* Function call */
  10.     sum = gt_func2(num1, num2); /* Function call */
  11.     printf(“sum = %d\n”,sum);
  12.     return 0;
  13. }
  14. void gt_func1() /* Function definition */
  15. {
  16.     printf(“Welcome to gyantoday.com\n”);
  17. }
  18. int gt_func2(int x, int y) /* Function definition */
  19. {
  20.     int total;
  21.     total = x + y;
  22.     return total; /* Function return */
  23. }

Explanation:-

  1. Here num1, num2 is actual argument and x, y is formal argument.
  2. In this program i have added one more function that is gt_func2() , after  successfully execution of  function gt_func1() compiler execute next line which line number 11. This is another function call after reading this function compiler will jump at line number 19 and execute function gt_func2() line by line , The return value is summation of  value x and y.
  3. Compiler return ‘total’ value and assign to variable ‘sum’ at line number 11. Suppose num1 and num2 is 10 and 40 respectively then after execution of program value of  ‘sum’ will be 50.

The output of above C program is :-
$ ./a.out
Welcome to gyantoday.com
sum = 50

So if we want to add any two integer, any numbers of time just we can call function gt_func2() and our work has been done. Here no need to write code again and again.

 

 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.