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...
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.
Problem: Grading Function #Write a function called getScore() that does not take any parameter. Inside the...
Problem: Grading Function #Write a function called getScore() that does not take any parameter. Inside the function, it asks the user to input scores for 3 items (Each of the items is out of 100). The final score is calculated by 20% of item1 + 30% of item2 + 50% of item3. After calculating the final score, the function returns it to the caller. The scores are floating-point numbers. #Write another function called getLetterGrade() that takes a float parameter and...
Find as many programming languages as possible which fit the following categories. When you find a...
Find as many programming languages as possible which fit the following categories. When you find a language which fits the category, indicate the following information: The name of the language The year the language was released A sample bit of code to prove that the language belongs to the given category. For full marks, you must find at least two languages for each category. To make the assignment more interesting, extra points will be awarded to the person who finds...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT