when transferring one function


When  transferring  one  function  to  another,  what  is  meant  by  the  guest  function?

A pointer to a function can be passed to another function as an argument. This allows one function to be transferred to another, as though the first function were a variable. This  is  very  useful  in  scientific  programming.  Imagine  that  we  have  a  routine  which numerically integrates a general one-dimensional function. Ideally, we would like to use this routine to integrate more than one specific function. We can achieve this by passing (to the routine) the name of the function to be integrated as an argument.

The function whose name is passed as an argument is called guest function. Likewise, the function to which this name is passed is called the host function. A pointer to a guest function is identified in the host function definition by an entry of the form data-type  (*function-name)(type 1,  type 2, ...) in the host function's argument declaration. Here, data-type is the data type of the guest function, function-name is the local name of the guest function in the host function definition, and type 1, type 2, ... are the data types of the guest function's arguments. The pointer to the guest function also requires an entry of the form data-type  (*)(type 1, type 2,  ...) in the argument declaration of the host function's prototype. The guest function can be accessed within the host function definition by means of the indirection operator. To achieve this, the indirection operator must precede the guest function name, and both the indirection operator and the guest function name must be enclosed in parenthesis: i.e.,

 

(*function-name)(arg 1, arg 2,  ...)

Here, arg 1, arg 2,... are the arguments passed to the guest function. Finally, the name of a guest function is passed to the host function, during a call to the latter function, via an entry like "function-name" in the host function's argument list.

The program listed below is an example which illustrates the passing of function names as arguments to another function:

/* passfunction.c */

/*

Program to illustrate the passing of function names as arguments to other functions via pointers

*/

#include

/* Function prototype for host fun. */

void cube(double (*)(double), double, double *);

double fun1(double);   // Function prototype for first guest function

double fun2(double);

int main()

 

{

// Function prototype for second guest function

double x, res1, res2;

/* Input value of x */ printf("\nx = "); scanf("%lf", &x);

/* Evaluate cube of value of first guest function at x */

cube(fun1, x, &res1);

/* Evaluate cube of value of second guest function at x */

cube(fun2, x, &res2);

/* Output results */

printf("\nx = %8.4f  res1 = %8.4f   res2 = %8.4f\n", x, res1, res2);

return 0;

}

void cube(double (*fun)(double), double x, double *result)

{

/*

 

Request for Solution File

Ask an Expert for Answer!!
Computer Engineering: when transferring one function
Reference No:- TGS0305532

Expected delivery within 24 Hours