Friday, 30 March 2012

Category of function


Category of function

A Function category may depends on whether arguments are presents or not and whether a value is returned or not. It can be divided in following category.

1. Function with No Argument and No return value.
2. Function with Argument and No return value.
3. Function with Argument and with return value.
4. Function with No Argument and with return value.

1.                Function with No Argument and  No return value
è    When a function has no argument, it does not receive any data from the calling function similarly when it does not return a value, the calling function does not receive any data from the called function.
è    There is no any data transfer between the calling function and called function.
Ex,
#include<stdio.h>
void sum();         //prototype
void main()
 {
          sum();                  //calling
 }
//body of function
void sum()
 {
          int a,b,c;
          printf(“\n Enter any No1 :”);
          scanf(“%d”,&a);
          printf(“\n Enter any No2 :”);
          scanf(“%d”,&b);
          c=a+b;
          printf(“\n  Sum =%d”,c);
}

          As shown in above example the function sum() is not receiving any value  & not returning any value to the main function.

2.Function With Arguments and No return values

è      In this type of user defined function the main function or calling function takes value and passes the values to the user defined function.
è      The function receive any data from the calling function but does not return  a value to the calling function.
è      When main() passing any arguments to the udf so the arguments in main() is called actual argument and received argument in function is called format argument The actual argument & format argument should match in number, type and order.The values of actual arguments are assigned to the format arguments on a one by one basis.

Ex
          void main()
          {
                   -----------            
                   function1(a1,a2,a3……….an);
                             ^                 ^
                       Function call      Actual argument
          }

          //Calling function
          function1(f1,f2,f3…………fn)
                                      ^
                                      Formal argument
{
                    -------------------
                    --------------------
}

          When function call is made only a copy of the values of actual arguments is passed into the called function.

Ex
          void sum(int,int);
          void main()
           {
                   int a,b,c;
                   printf(“\n Enter any No :”);
                   scanf(“%d”,&a);
                   printf(“\n Enter any No :”);
                   scanf(“%d”,&b);
                   sum(a,b);
          }

          void sum(int x,int y)
           {
                   int z;
                   z=x+y;
                   printf(“\n sum %d”,z);
           }

          Is shown in above example main() send two argument of int data type to udf calculates its sum & prints it on the screen.

3.Function With Argument and With Return Values

è    In this type of the called function receive any data from the calling function or main function, process on it and also returning back to the calling function .
è    This is the two way data communication process.
Ex
          int sum(int,int);
          void main()
           {
                   int a,b,c;
                   printf(“\n Enter any No :”);
                   scanf(“%d”,&a);
                   printf(“\n Enter any No :”);
                   scanf(“%d”,&b);

                   c=sum(a,b);
                   printf(“%d”,c);
          }
          int sum(int x,int y)
           {
                   int z;
                   z=x+y;
                   return(z);
          }

          As shown in above example main() sends two arguments of int data type to udf & udf calculate the sum  and return back answer to calling function in main and print it.

4. Function with No Argument and  with return value.

è    In this type of function main function or calling function dies not pass any value to the called function.
è    The called function itself manage the task of getting data, process on it and return the value back to the calling function.
è    Here, the function may not take any value but returns a value to the calling function.

Ex, int sum();
          void main()
           {
                   int c;
                   c=sum();
                   printf(“%d”,c);
          }
          int sum()
           {
                   int z,x,y;
                   printf(“\n Enter any No :”);
                   scanf(“%d”,&x);
                   printf(“\n Enter any No :”);
                   scanf(“%d”,&y);
z=x+y;
                   return(z);
          }

Posted By : Ruchita Pandya

No comments:

Post a Comment