Friday, January 1, 2010

8.9 PASSING FUNCTIONS TO OTHER FUNCTIONS

A pointer to a function can be passed to another function as argument.This allows transferring of one function to another, as if the first function were a variable.let us refer the first function as guest function, and the second function as host function.

The formal argument declaration of the guest function can be written as

data-type (*function-name) ( )

where data-type refers to the data type of the quantity returned by the guest and function-name is the name of the guest. The formal argument declaration can also be written as


data-type (*function-name) ( type 1, type 2,..... )

or as
data-type (*function-name) ( type 1 arg 1, type 2 arg 2,..... )

where  type 1, type 2,..... refers to the data types of the arguments associated with the guest; arg 1, arg 2,....... refers to the names of  the arguments associated with the guest.

The host function declaration can now be written as

funct-data-type funct-name
                       (arg-data-type (*pt-var) ( type 1 arg 1, type 2 arg 2,..... ),
                       |<---     pointer to guest function               ----------->|
                                                     data types and names of other funct args);

where  funct-data-type refers to the data type of the quantity returned by the host function; funct-name refers to the name of host function; arg-data-type refers to the data type of the quantity returned by the guest function; pt-var refers to the pointer variable pointing to the guest function, and
type 1 arg 1, type 2 arg 2,..... refer to the data types and the corresponding names of the guest function's arguments.


The skeletal outline of a C program is shown below.This program consists of four functions: main, process, funct1 and funct2.Note that process is host function for funct1 and funct2. Each of the three subordinate functions returns an integer quantity.

int process(int (*)(int, int));       /*function declaration (host)*/
int funct1(int, int);                    /*function declaration (guest)*/

int funct2(int, int);                   /*function declaration (guest)*/

main()
{
int i, j;
...................
i = process(funct1);         /*pass funct1 to process; return a value for i*/
...................


j = process(funct2);        /*pass funct2 to process; return a value for j*/
...................


}


process(int (*pf)(int, int))    /*host function definition*/
                                        /*(formal argument is a pointer to a function */
{
int a, b, c;
...................  


c = (*pf)(a, b);             /*access the function passed to this function; return a value for c */
...................  


return(c);

}


funct1(a, b)      /*guest function definition*/

int a, b;

{
int c;

c=.........       /*use a and b to evaluate c*/


return(c);


}

funct2(x, y)      /*guest function definition*/

int x, y;

{
int z;

z=.........       /*use x and y to evaluate z*/


return(z);


}

No comments:

Post a Comment