All submissions

Function Pointer apply

#include <iostream>
#include <string>
using namespace std;

int square(int x) {
    return x * x;
}

int cube(int x) {
    return x * x * x;
}

// define apply(int, function pointer)
void apply(int n, int (*func)(int)) {
    cout << func(n) << endl;
}

int main() {
    int n;
    string opName;
    cin >> n >> opName;

    if (opName == "square") {
        apply(n, square);
    } else if (opName == "cube") {
        apply(n, cube);
    }

    return 0;
}
Loading...

Input

5 square

Expected Output

25