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

Common tasks to accomplish - artificial intelligence, Common Tasks to Accom...

Common Tasks to Accomplish: Once you've worried about what and why you're doing "AI", what has inspired you and how you're going to approach the job, then you just start to th

Reading decision trees - artificial intelligence, Reading Decision Trees ...

Reading Decision Trees There is a link between decision tree representation and logical representations, which may be exploited to form it more easy  to understand and learned

Name the two operations of stack, Name the two operations of stack A st...

Name the two operations of stack A stack has only two operations and they are insertion and deletion of items. The operation insertion is called push (or push-down) as it can b

Explain bootp (boot strap protocol), Explain BOOTP (Boot Strap Protocol). ...

Explain BOOTP (Boot Strap Protocol). TCP or IP designer observed that several of the configuration steps could be combined in a single step if a server was capable to supply mo

Future of hyper threading, Current Pentium 4 based MPUs use Hyper-threading...

Current Pentium 4 based MPUs use Hyper-threading, but the next-generation cores, Woodcrest and Merom, Conroe will not. While some have alleged that this is because Hyper-threading

What is e-cash, E-Cash:  E-cash is cash shown by two models. One is the...

E-Cash:  E-cash is cash shown by two models. One is the on-line form of e-cash (introduced by DigiCash) which permits for the completion of all types of internet transactions.

Matlab, Use the colon operation to create a vector x of numbers -10 through...

Use the colon operation to create a vector x of numbers -10 through 10 in steps of 1. Use matrix operations to create a vector y where each element is 5 more than 2 times the corre

Explain the term - instruction execution, Explain the term - Instruction ex...

Explain the term - Instruction execution We  know  that  the  fundamental  function  performed  by  a  computer  is  the  execution  of  program. The program that is to be exec

Programming with loops and comparisons, Q. Programming with loops and compa...

Q. Programming with loops and comparisons? This segment deals with more practical illustrations employing comparison, loops and shift instructions. Simple Program Loops

Give an example of first fit and best fit algorithms, Given memory partiti...

Given memory partitions of 100k, 500k, 200k, 300k, and 600k (in order), apply first fit and best fit algorithms to place processes with the space requirement of 212k, 417k, 112k an

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