What is function in c

We can define function as a block of code which has a name and a special task , we can call it from any where of c program using its name .
If you are not getting the point by its definition then consider on a simple function "printf()" 
what is printf()   ??
its a function and according to definition it has a name(printf) and specific work (to print on monitor screen ).
it's a system define function but we can also define our own function .
So we can categories function in two type

1.System define function . 
Ex- printf(), scanf(),gtes, puts, strcpy() ....etc

2. User define function 
 Ex-    whatever name and task we give 

let's go for write own function and understand how it works ,how many types of aspects of it 

main()
 {
   printf("Implement of function");
   user();
   }
user()
{
   printf("This is the simplest one");
 }
look on this code segment , simplest example of function calling .
Since main() is a function and execution begins from main  actually,  so in every program main must be present .

here execution begins from main and print the message 
Implement of function
and now we are invoking function user(Define by user)
So control goes to user and print the message 
"This is the simplest one "
after this control goes back to main and program terminate .


If we define own  function in any program there should be three part of the function 
1. Function prototype  
2. Function call 
3. Function definition   

program that will  make you familiar to these terms  

Program to sort the element of array .(Bubble sort)

#include<stdio.h>
#include<alloc.h>
  int *array,n;
void sort_array();  // Function prototype 
int main()
 {
 int i;
printf("How many element you want in array \n");
scanf("%d",&n);

array=(int*)malloc(sizeof(int)*n);

printf("Enter the element into array\n"); 

  for(i=0;i<n;i++)
scanf("%d",(array+i)); 

// array before sorting 
  printf("See what you entered\n  ");

 for(i=0;i<n;i++)
printf("%d\n",*(array+i)); 



sort_array();           // function calling 

// array after sorting (after calling the function sort_array()) 
printf("Array after sorting \n ");
  for(i=0;i<n;i++)
printf("%d\n",*(array+i));  // a[i] will also work 

return 0 ;
}
void sort_array()    // Function definition 

{
 int i, j,temp;
for(i=0;i<n;i++)
 {
for(j=i;j<n;j++)
{
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
 array[j]=temp;
}
 }
 }  
}  // closing of function 


If you go through the program you will see all three terms function prototype, function call and function definition .

function prototype :- it's a layout of function we are going to use in our program ,using this compiler knows that what is return type of function, how many argument it has . compiler makes a table of information about this function and keep for future use .

function call :  - we use the function name  to transfer the control to the function definition ,as in program , after executing it control goes to definition of function and complete its work !! 

function definition : - it's body of function i.e. what is the work of function we write in body of function(definition ) after executing the name of function control comes to function definition
Share on Google Plus

0 comments: