Challenge: Write your First Higher-Order Function

Test yourself and implement what you have learned so far in this challenge.

Problem Statement

You need to create a higher-order function arithmeticPrinter which prints the result of an arithmetic function that has two parameters of type Int and returns a value of type Int.

In this challenge, you will assume that the following arithmetic functions have been declared:

def add(a: Int, b: Int): Int = {
  a + b

def subtract(a:Int, b:Int) = {
  a - b

def multiply(a:Int, b:Int) = {
  a * b

def divide(a:Int, b:Int) = {
  a / b

For instance, the arithmeticPrinter will take the add function as input and print its result.


arithmeticPrinter has three parameters.

  1. A function f which has two parameters of type Int and returns a value of type Int.
  2. An integer x
  3. An integer y

The input will be a function and two integers that will be passed to the function.


The output will be the result of the arithmetic function.

Sample Input

add, 4, 9

Sample Output


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