What is meant by the guest function, Computer Engineering

Assignment Help:

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)

{

/*

 


Related Discussions:- What is meant by the guest function

Explain the working of hundred-line exchange, In a hundred-line exchange 24...

In a hundred-line exchange 24 two-motion selectors are used. Draw the schematic you suggest for this exchange and explain its working. How many simultaneous calls can be made durin

Computer grphics, Advantages of DDa algorithms for line drawimg

Advantages of DDa algorithms for line drawimg

Describe architecture of wap gateway, Describe architecture of WAP gateway....

Describe architecture of WAP gateway. WAP GATEWAY : It is a very unique product giving semi-automatic redirection of HTML documents to WAP compatible mobile phones. Wir

Explain priority encoder with an example, Explain Priority encoder with an ...

Explain Priority encoder with an example. Ans. Priority encoder- Basically an encoder is a combinational circuit which performs the inverse operation of a decoder. The inp

What do you understand by scan codes, Q. What do you understand by Scan Cod...

Q. What do you understand by Scan Codes? A scan code is a code produced by a microprocessor in keyboard when a key is pressed and is unique to key struck. When this code is rec

Draw and elucidate the block diagram 8259, Draw and elucidate the block dia...

Draw and elucidate the block diagram of programmable interrupt controller 8259. The 8259A adds 8 vectored priority encoded interrupts to microprocessor. It can be expanded to 6

Why is fragmentation needed on internet not on a typical wan, Why is fragme...

Why is fragmentation needed on Internet not on a typical WAN? TCP/IP protocol utilizes the name IP datagram to demote to an Internet packet. The amount of data carried into a d

How does multiplexer know which line to select, How does multiplexer know w...

How does multiplexer know which line to select? This is managed by select lines. The select lines provide communication among different components of a computer. Now let's see

Define a formal system, Q. Define a Formal System? A Formal System is o...

Q. Define a Formal System? A Formal System is one which is planned in advance and is used according to schedule. In this system procedures and policies are documented well in a

Data types done between abap/4 & external level, How is conversion of data ...

How is conversion of data types done between ABAP/4 & external level? Conversion among the external layer and the ABAP/4 layer is completed in the SAP dialog manager DYNP.

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd