Types of functions


Functions can be of different types:-

1.      Functions without parameters, without return value.
2.      Functions with parameters, with return value.
3.      Functions with parameters, without return value.
4.      Functions without parameters, with return value.

i)  Functions with parameters, with return value:-
These are the functions which pass values from functions call to function definition and take the return value from called function to the calling function.

Eg prog:-         int sum (int a, int b);
                        void main ( )
                        {                      int x,y,z;
                     printf (“ Enter any 2 values”);
                     scanf (“%d%d”, &x,&y);
                     z=sum(x,y);  /* function call*/
                     printf(“sum is %d”, z);
                        }
                        int sum (int a, int b)
                        {        int c;/* Local variable declaration*/
                        c=a+b;
                        return (c);       
}




(ii) Function with parameters, without return value:-

ü  In these functions, values are passed from calling function to the called functions but no value is returned from called function to calling function.      The representation of no return value is done with the data type ‘ void’.

Eg prog:-         void sum (int a, int b);
                        void main ( )
                        {
                             int x,y;
                                printf (“ Enter any 2 values”);
                                scanf (“%d%d”, &x,&y);
                         sum(x,y); 
                        }
                        void sum (int a, int b)
                        {       int c;
                               c=a+b;
                            printf(“sum=%d”,c);
                        }

iii) Functions without parameters, with return value:-

ü  These are the functions which do not take any values from the calling function but return value to it.
ü   

Eg prog:-          int sum ( );
                        void main ( )
                        {
                            int z;
                                    z=sum(x,y); 
                                    printf(“sum=%d”,z);
                        }
                        int sum ()
                        {
                                     int a,b;
                                    printf(“enter 2 values”);
                                    scanf(“%d%d”,&a,&b);
return(a+b);
                        }


iv) Functions without parameters, without return value:-

ü  Functions without parameters indicate that no value are passed from calling function to the called function and no return value indicates that the called function will not give any data back to the calling function.



Eg prog:-         void sum ( );
                        void main ( )
                        {
                                    clrscr( );
                                     sum(x,y); 
                                   getch( );
                        }
                        void sum ()
                        {
int a,b;
printf(“enter 2 values”);
scanf(“%d%d”,&a,&b);
                     printf(“sum=%d”,(a+b));
                        }

No comments:

Post a Comment