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

Temporary registers w and z, Why the temporary registers W and Z are named ...

Why the temporary registers W and Z are named so I mean we start from A,B,C,D,E then H and L coz H stands for higher bit nd L for lower bit of the address pointed by memory pointer

Features of hyper-threading, The salient features of hyper threading are: ...

The salient features of hyper threading are: i)  Improved support for multi-threaded code, permitting multiple threads to run concurrently. ii) Response time and improved rea

Determine the boolean expression using k-map, Reduce the following equation...

Reduce the following equation using k-map Y = BC‾D‾ + A‾BC‾D + ABC‾D + A‾BCD + ABCD Ans. Multiplying the first term with (A+A') Y = A'BC'D' + ABC'D' + A'BC'D + ABC'D + A'BCD +

How did you find web server related issues, Using Web resource monitors we ...

Using Web resource monitors we can search the performance of web servers. Using these monitors we can examine throughput on the web server, number of hits per second that happened

Explain booth''s multiplication algorithm, Computer Organization 1. Dr...

Computer Organization 1. Draw a flowchart of a Booth's multiplication algorithm and explain it. 2. What is the concept of memory interleaving? 3. Explain virtual memory? Expla

What is analysis and list its sub stages, What is analysis, list its sub st...

What is analysis, list its sub stages? Understand the requirement by constructing models. The goal of analysis is to state what need is to be done and not how it is to be done.

How future environment changes can be predicted - simulation, How future cl...

How future climate/environment changes can be predicted -  Information over time is fed into a weather/climate model -  based on changes in weather patterns, carries out a s

Read after write and write after write - data hazards, RAW  and WAW - Data ...

RAW  and WAW - Data hazards: RAW (read after write) - j tries to read a source before i writes it, hence j wrongly gets the old value .This is the most usual type of

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