What is Function

Friday, April 4, 2014

What is function in C?
A function is a self-contained block of statement that perform a coherent task of some kind.
There are two types of function in C:
  1. Library function or In-built function
  2. User define function

Structure of function in C program

/* demonstration of function status in program */  

#include<stdio.h>                                  
void disp();   /* function prototype declaration */
int main()                                         
{                                                  
  disp();      /*function calling*/                
  printf("C is Awesome!!!");                       
  return 0;                                        
}                                                  
void disp()    /* function definition */           
{                                                  
  printf("Hi, ");                                  
}                                                  

The output of above program would be:

Hi, C is Awesome!!!

Lets grab some useful info from the above program:
In above program we have 2 function:
main()
disp()

Here you can see and thought:
1. why main() is used only once in program?
2. why disp() is used three times in program?

The answer of first question is "if a c program contains only one function then it must be main()." i.e. every C program must have main() function.

The answer of second question: lets understand the meaning of every disp() in program:

void disp();
this is the first disp() that used in program. It is function prototype.
This prototype tells the compiler that as:
What is function type ( i.e. void or not ),
What is name of function( i.e. in our program function name is disp)
Both parenthesis () indicate that disp is function.
keep in mind: void word indicating that "nothing return".

disp();
this is the second disp() that used in program. It is function calling.
Every calling function main work is as:
when compiler reached at calling function disp(); program control passes to the function disp(), (i.e. activity of main() is temporary suspend ), when disp() function runs out all his statement execute, the control returns to the main() function and begins executing code at the exact point where it is left off.


void disp()
{
  printf("Hi, ");
}

this is the third disp() that used in program. It is function definition.
In function definition we can used all C language keywords, operators, variable etc.
In above program recently we have only 1 printf(); statement.

Lets exercise, what we learn:

/*exercise of function calling C program*/
#include<stdio.h>
void fun1();
void fun2();
void fun3();
int main()
{
  printf("
I am in main function!!"
);

  fun1();
  fun2();
  fun3();
  return 0;
}
void fun1()
{
  printf("
I am in fun 1"
);

}


void fun2()
{
  printf("
I am in fun 2"
);

}


void fun3()
{
  printf("
I am in fun 3"
);

}


The output of above program would be:

I am in main function!!
I am in fun 1
I am in fun 2
I am in fun 3


Related Posts by Categories

0 comments:

Post a Comment

Copyright © 2014 Information Sharing | Powered By Blogger