Saturday, 26 May 2012

overview of functions

function is a way through which we separate our code into different sections. this functionality gives power to reuse that section again and again anywhere in code.

Situation :  In our community we have different profile for people that works as designation for them like doctor, engineer, worker etc. All these work in different section. but whenever we need any one of them we can take help from them.

Point we get   above situation we can apply in programming that's know as function. because all works as a different designation. Same we can define functions for use of different purpose and according to our need we call any  function at any time.


Parts of function : we need to use three basic rule to define a function in c-language.

  1. function prototype 
  2. function call
  3. function body
Function prototype : This part works as function declaration in programming. This step works as registration .

As we want to participate in any function. Before going there we need to inform them.
this step in programming called function prototype or function declaration.

Pattern
                         return type  function-name(function-arguments);
         

return-type: what value we want to return from function
                 
                          int          ----------  for integer
                          char      ----------   for character
                          float      ----------   for float 
                          void       ----------   for not returning values

function-arguments this step tell what parameter we want to pass to function and values are passed as simple variable declaration form.


                            void  add(int a ,int b);

 Function Call  This step is used to call a function. It is used to in following manner.

  Pattern          
                        function-name(arguments);
                        add(a,b);

Function body  This step is used to define how this function work . In this part we define whole purpose of function .

 Pattern 
                      return-type  function-name(arguments)
                      {


                      }


from above discussion we study about basic rule how to define and use function in our programming.

Example : 

Addition of two number without function

   #include<stdio.h>
   int main()
   { 
      int i=2 , int j =5;
      c=i+j;
      printf("addition of i and j is \t");
      printf("%d\n",c);
      i=10;
      j=20;
      c=i+j;
      printf("addition of i and j is \t");
      printf("%d\n",c);
      i=100;
      j=200;
      c=i+j;
      printf("addition of i and j is \t");
      printf("%d\n",c);
     
      getch();
   }

Here if want add two number with different values we need to repeat same type of code again and again . which is wastage of programming logic. so how we resolve this situation.
  
Addition of two number using function

   #include<stdio.h>
   void add(int a, int b)
   int main()
   { 
      int i=2 , int j =5;
      add(i,j);
      i=10;
      j=20;
      add(i,j);
      getch();
    }
   void add(int a, int b)
   {
     int c;
     c=a+b;
     printf("%d",c);
   }


No comments:

Post a Comment