Search⌘ K
AI Features

Challenge: Absolute Value

Explore how to define and implement a Scala function that calculates the absolute value of a given Double. This lesson helps you strengthen your understanding of function creation and application in Scala, supporting your development of effective functional programming skills.

Problem Statement

You need to create a function absolute which computes the absolute value of a given number.

Input

The input of the function is a number x of type Double whose absolute value you want to compute.

Output

The output will be the absolute value of x.

Sample Input

-5

Sample Output

5

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!

Scala
def absolute(x: Double): Double = {
// Write your code here
return -1 // Remove this line after writing your code
}

Let’s go over the solution review in the next lesson.