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

Microprocessor, what is opcode for addition and subtraction

what is opcode for addition and subtraction

Explain the potential role of information technology, Question 1: Descr...

Question 1: Describe the five maturity levels of KM that an organization faces when adopting the Frid's KM framework. Question 2: (a) Describe three major issues that

C++, explain parallel database system

explain parallel database system

Explain auto increment and auto decrement mode, Explain Auto increment and ...

Explain Auto increment and Auto decrement mode  The register is incremented or decremented after (or before) its value is used to access memory.  The address stored in the regi

Explain direct memory access, Explain Direct Memory Access. A modest en...

Explain Direct Memory Access. A modest enhances in hardware enables an IO device to transfer a block of information to or from memory without CPU intervention. This task needs

Data reader by returned from a web services, Why can not data reader by ret...

Why can not data reader by returned from a Web Services Method? Ans) Due to, it is not serializable

The long and short date option , In MS Access, the long and short date opti...

In MS Access, the long and short date option does not show 4 digit years. How do I achieve the mm/dd/yyyy format? Ans) In the date field properties of the table, form, or prop

Define TII, TII stands for? Ans. TII stands for Table of incomplete ins...

TII stands for? Ans. TII stands for Table of incomplete instructions.

Compact disc - computer storage, Compact Disc; Compact Disc ( CD)   i...

Compact Disc; Compact Disc ( CD)   is an optical disc  which is used to  store digital data. It was initially developed to store sound recordings completely, but later it als

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