Passing Function name as arguments to a function
Asked Answered
B

5

7

Is it possible to pass the name of a function(say A) as an argument to another function (say B), and then call function A from function B. That is the function name will be stored in a variable in B, and using it call the function whose name is in the variable. For Example in C++ sort function the first and second arguments are iterators, but the third argument is the name of a function.

Bernoulli answered 15/6, 2011 at 10:26 Comment(1)
Related: #47502568Neoplasty
F
9

You can introduce a template parameter (here Pred) for "anything that is callable with two parameters":

template <typename Iter, typename Pred>
void mysort(Iter begin, Iter end, Pred predicate)
{
    --end;
    // ...
        if (predicate(*begin, *end))
        {
            // ...
        }
    // ...
}

Then you can pass either good old C function pointers or C++ function objects:

bool function(int x, int y)
{
    return x < y;
}

struct function_object
{
    bool operator()(int x, int y)
    {
        return x < y;
    }
};

int main()
{
    int numbers[] = {543, 6542654, 432514, 54, 45, 243};
    mysort(numbers + 0, numbers + 6, &function);
    mysort(numbers + 0, numbers + 6, function_object());
}

As you can see, a function object is an object of a class that overloads operator() appropriately.

Funke answered 15/6, 2011 at 10:45 Comment(3)
mysort(numbers + 0, numbers + 6, function); should also work, since "a function is implicitly convertible to a pointer to itself".Herodotus
Can we use the template method for functions requiring 4-5 parameters?Lock
@AlphaMineron Sure, the parameter Pred predicate does not state any requirements on the type Pred. Only through the usage if (predicate(*begin, *end)) will the compiler check that it is callable with 2 arguments. If you need 5 arguments, then simply call it with 5.Funke
H
5

You should read up on Function Pointers.

Hyperion answered 15/6, 2011 at 10:27 Comment(0)
C
5

simple example with Function Pointers:

#include <iostream>

int apply(int (*fun)(int,int), int a, int b) {
    return (*fun)(a,b);
}

int add(int a, int b) {return a + b;}
int multiply(int a, int b) {return a * b;}

int main(int argc, const char* argv[]) {
    int added = apply(add, 2, 4);
    int multiplied = apply(multiply, 2, 4);

    std::cout << "added result: " << added << std::endl;
    std::cout << "multiplied result: " << multiplied << std::endl;
}

output:

added result: 6
multiplied result: 8
Confocal answered 27/10, 2013 at 8:1 Comment(0)
P
3

Yes, not only is it possible, but has great usage as well.

Technically, these are known as function pointers.

Conceptually, they are required to have callback mechanisms. Callbacks might be required for event-driven programs. E.g., your application might be interested to know when a mouse-button is clicked. In this case you would register your interest to the underlying platform for a mouse click event, and tell it what method/function should be called in your program, so that you can execute code accordingly.

Another such case is where programs have an asynchronous mode of execution. E.g., in case a file is to be written to hard disk, which is a time consuming process as compared to doing arithmetical calculations. So, it might be a case where we do not want to wait performing calculations while we are writing results to a file. A program might choose to just call a write function on a file and return and start calculations. While, calling this function the caller might also specify a callback function. So, when the file is written successfully or fails, the callback function is called and the callee informed accordingly.

Prader answered 15/6, 2011 at 10:44 Comment(0)
T
2

Yes, it's possible. Technically, it's not quite the 'name' that's passed, but rather, actually a pointer to the function being passed.

Toxophilite answered 15/6, 2011 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.