Question

In: Computer Science

When a function can take another function as a parameter, how can type-checking occur? (Programming languages)

When a function can take another function as a parameter, how can type-checking occur?

(Programming languages)

Solutions

Expert Solution

You cannot pass function to another function as parameter. But, you can pass function reference to another function using function pointers. Using function pointer you can store reference of a function and can pass it to another function as normal pointer variable.

When you pass as a pointer that you need to declare the type of function in parameter itself.

#include <stdio.h>


/* Function declarations */
void greetMorning();
void greeEvening();
void greetNight();

void greet(void (*greeter)());



int main()
{
    // Pass pointer to greetMorning function 
    greet(greetMorning);


    // Pass pointer to greetEvening function 
    greet(greeEvening);


    // Pass pointer to greetNight function 
    greet(greetNight);


    return 0;
}


/**
 * Function to print greeting message.
 * 
 * @greeter     Function pointer that can point to functions
 *              with no return type and no parameters.
 */
void greet(void (*greeter)())
{
    // Call greeter function pointer
    greeter();
}



void greetMorning() 
{
    printf("Good, morning!\n");
}


void greeEvening() 
{
    printf("Good, evening!\n");
}


void greetNight() 
{
    printf("Good, night!\n");
}

Output

 

 

Related Solutions

Some languages support many modes of parameter passing. Provide 2 examples using two different programming languages...
Some languages support many modes of parameter passing. Provide 2 examples using two different programming languages which support the user of the programming language deciding which to use when creating their method. (Programming Languages)
Write a function for checking the speed of drivers. This function should have one parameter: speed....
Write a function for checking the speed of drivers. This function should have one parameter: speed. 1. If speed is less than 70, it should print “Ok”. 2. Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points. For example, if the speed is 80, it should print: "Points: 2". 3. If the driver gets more than 12 points, the function should print: “License suspended”...
When can multiple optimal solutions can occur in linear programming problems? Explain.
When can multiple optimal solutions can occur in linear programming problems? Explain.
Which of the types listed below can be the type of a function output parameter? unsigned...
Which of the types listed below can be the type of a function output parameter? unsigned long * double unsigned long char * Void Consider this function that takes a fraction (with arguments for numerator and denominator) and inverts it in place: void invert_fraction(type num, type denom) {    int tmp = *num;    if (*num == 0 || *denom == 0) {        fprintf(stderr, "ERROR: cannot invert %d/%d.\n", *num, *denom);        exit(1);     }     *num = *denom;    ...
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It...
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It "pretty prints" the contents of the namedtuple, including both the names of its fields and their values. This is subject to a few rules. Each field and its value are displayed on one line, with the name of the field appearing first, followed by a colon and a space, followed by the value of the field converted to a string. The fields should be...
Create a function named ‘dividing’. This function will take one parameter value, which will be an...
Create a function named ‘dividing’. This function will take one parameter value, which will be an integer. For the function you will use ‘Exception Handling’ with the use of the try and except statements to try and return the results of a number divided by the parameter value given to the function. If the parameter passed to the function is the integer value of zero then your function should return ‘You tried to divide by zero!’ through the use of...
In Python Complete the function powersOf5(). Have the function take a parameter n and return a...
In Python Complete the function powersOf5(). Have the function take a parameter n and return a list of the first n powers of 5. One way to calculate powers of 5 is by starting with 1 and then multiplying the previous number by 5. Examples: powersOf5( 1 ) returns [1] powersOf5( 2 ) returns [1, 5] powersOf5( 3 ) returns [1, 5, 25] powersOf5( 6 ) returns [1, 5, 25, 125, 625, 3125] powersOf5( 10 ) returns [1, 5, 25,...
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list. (please do on racket language)
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list.
In Python, Complete the longestWord() function to take a list as a parameter and return the...
In Python, Complete the longestWord() function to take a list as a parameter and return the length of the longest word in that list. Examples: longestWord(['Python', 'rocks']) returns 5 longestWord(['Casey', 'Riley', 'Jessie', 'Jackie', 'Jaime', 'Kerry', 'Jody']) returns 6 longestWord(['I', 'a', 'am', 'an', 'as', 'at', 'ax', 'the']) returns 3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT