Challenge: Write your First Higher-Order Function
Test yourself and implement what you have learned so far in this chapter.
We'll cover the following
Problem statement
You need to create a higher-order function, arithmeticCalculator
, which returns the result of an arithmetic function that has two parameters of type int
and returns a value of type num
.
In this challenge, you will assume that the following arithmetic functions have been declared:
num add(int a, int b) {
return a + b;
}
num subtract(int a, int b) {
return a - b;
}
num multiply(int a, int b) {
return a * b;
}
num divide(int a, int b) {
return a / b;
}
For instance, the arithmeticPrinter
will take the add
function as input and return its result.
Input
arithmeticPrinter
has three parameters.
- A function,
f
- An integer,
x
- An integer,
y
The input will be a function and two integers that will be passed to the function.
Output
The output will be the result of the arithmetic function.
Sample input
add, 4, 9
Sample output
13
Test yourself
Write your code in the given area. Try the exercise by yourself first, but if you get stuck, the solution has been provided. Good luck!
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy