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
- Library function
- 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
- #include<stdio.h>
- int main( )
- {
- gt_func1( ); /* Function call */
- return 0;
- }
- void gt_func1( )
- {
- printf(“Welcome to gyantoday.com\n”);
- }
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.
- #include<stdio.h>
- void gt_func1( ); /* Function prototype */
- int main( )
- {
- gt_func1( ); /* Function call */
- printf(” Returning from function gt_func1 \n”);
- return 0;
- }
- void gt_func1( )
- {
- printf(“Welcome to gyantoday.com\n”);
- }
Explanation:-
- Here main() function is calling function and function gt_func1() is called function.
- 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.
- 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.
- 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
- #include<stdio.h>
- void gt_func1(); /* Function prototype */
- int gt_func2(int x, int y); /* Function prototype */
- int main()
- {
- int num1 = 10;
- int num2 = 40;
- int sum = 0;
- gt_func1(); /* Function call */
-
- sum = gt_func2(num1, num2); /* Function call */
- printf(“sum = %d\n”,sum);
- return 0;
- }
- void gt_func1() /* Function definition */
- {
- printf(“Welcome to gyantoday.com\n”);
- }
- int gt_func2(int x, int y) /* Function definition */
- {
- int total;
-
- total = x + y;
- return total; /* Function return */
- }
Explanation:-
- Here num1, num2 is actual argument and x, y is formal argument.
- 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.
- 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. |