Passing function as elements in an Array

closed account (y6q2y60M)
Can we pass functions as elements of an array .

please reply fast.
Can you elaborate the question?
closed account (y6q2y60M)
@keskiverto

[code]

int calc[] = { simple() , scientific() , areavolume() , permutation() , combination() , simpleinterest() , compound() , unitcon() , matrix() , at() , datecalc() }

all these element of the array 'calc' are functions .

but i guess this is not not allowed so can you provide me with any solution that how can i run those function as the whole program is a menu driven program and the array 'calc' is called inside a for loop but as the functions are not allowed the main things wont run.


please help :(
all these element of the array 'calc' are functions .
They are not. calc is an array of 11 integers initializated with bunch of function calls.

Here is minimal example on array containing functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

void foo()
{
    std::cout << "foo\n";
}

void bar()
{
    std::cout << "bar\n";
}

int main()
{
    //Type aliases are recommended when you are dealing with function types
    using func_t = void(void);
    func_t* ops[] = {foo, bar};
    ops[0](); //Call function in array
    ops[1]();
}


Here is more complex example using function objects:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <functional>
#include <array>


int main()
{
    std::array<
        std::pair<std::string, std::function<double(double, double)>>,
        4
    > operations {{ //Note that () here creates object, not calls a function
                    //As all those are function objects, not functions
        {"Add",      std::plus<double>()},
        {"Subtract", std::minus<double>()},
        {"Multiply", std::multiplies<double>()},
        {"Divide",   std::divides<double>()},
    }};
    std::cout << "Enter operands: ";
    double x, y;
    std::cin >> x >> y;
    std::cout << "What do you want to do with them?\n";
    int n = 0;
    for(const auto& e: operations)
        std::cout << ++n << ' ' << e.first << '\n';
    std::cin >> n;
    std::cout << operations.at(n-1).second(x, y);
}
http://coliru.stacked-crooked.com/a/6275a6d721c034c1
closed account (y6q2y60M)
@Miinipaa

Here I am dealing with turbo c++ so can you tell me the method for calling the function in array for Turbo .

really appreciate your help .
please help me for this.
I am dealing with turbo c++
If you can, het another compiler. Turbo C++ is non-standard ancient compiler which didnt update for 20 years. You will have to learn C++ again if you ever intend to ever actually use it (and not get credit for it and forget all about C++).

For prehistoric compilers this should work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

void foo()
{
    printf("foo\n");
}

void bar()
{
    printf("bar\n");
}

int main()
{
    //Type aliases are recommended when you are dealing with function types
    typedef void(*func_p)();
    func_p ops[2] = {foo, bar};
    ops[0](); //Call function in array
    ops[1]();
    return 0;
}
closed account (y6q2y60M)
@Miinipaa

I know turbo c++ is really old compilor but according to our academics we have to use it as our compilor and even i hate it.

anyways thank you so much for your help> :)
Topic archived. No new replies allowed.