Passing Function name as arguments to a function

13,297

Solution 1

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.

Solution 2

You should read up on Function Pointers.

Solution 3

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

Solution 4

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.

Solution 5

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

Share:
13,297
Balanivash
Author by

Balanivash

Hey, am a guy, whose heart lies in anything that has to do with computers. Kinda workaholic if it comes to working with comps, but for anything else don't depend on me to do anything unless you have asked me till you get tired and do it yourself..:) . Have tried my hands in design, building websites, applications, organizing workshops, events etc.. and still exploring anything that has to do with computers , and mainly FOSS and linux based. #SOreadytohelp

Updated on June 29, 2022

Comments

  • Balanivash
    Balanivash almost 2 years

    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.

  • Mihai Todor
    Mihai Todor about 9 years
    mysort(numbers + 0, numbers + 6, function); should also work, since "a function is implicitly convertible to a pointer to itself".
  • Alpha Mineron
    Alpha Mineron almost 7 years
    Can we use the template method for functions requiring 4-5 parameters?
  • fredoverflow
    fredoverflow almost 7 years
    @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.