10.17.2012

What is Function

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!!!

Let's 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: let's 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.

Let's exercise, what we learn:

/*exercise of function calling C program*/
#include<stdio.h>
void fun1();
void fun2();
void fun3();
int main()
{
  printf("\nI am in main function!!");
  fun1();
  fun2();
  fun3();
  return 0;
}
void fun1()
{
  printf("\nI am in fun 1");
}


void fun2()
{
  printf("\nI am in fun 2");
}


void fun3()
{
  printf("\nI 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


3 comments:

  1. Q. #include
    #include
    void main()
    {
    float a= 12.25, b= 12.52;
    if(a=b)
    printf("\na and b are equal");
    getch();
    }


    Output : a and b are equal.

    Now my ques. is how they are equal???...whereas a is 12.25 and b=12.52

    ReplyDelete
    Replies
    1. @Gurpreet,
      In above program, statement if(a=b) is not correct condition, it is non-zero value i.e. condition is always true and compiler goes to immediately next line and produced output.
      so following statement is always true: if(1), if(a), if(b) etc..

      Keep in mind: Non-Zero values in C language is always True.

      correct condition: if(a==b)

      Delete
    2. The if(a=b) in that a=b not a test condition a=b means value of
      b is assigned to a because of that the compiler goes to next statement.
      In the above program if(a=b) is replace by if(a==b) then output
      is else statement in the program

      Delete