# Back2Basics: Understanding Partial Functions – #1 In this blog, we will talk about partial functions. We know about functions in Scala. Let’s talk about it first.

So, what is a function?

A function is a mapping or a transformation of an input to an output.

For example,  we have function isEven, which takes an int value and retuns boolean value.

```scala> def isEven(x :Int) = x % 2 == 0
isEven: (x: Int)Boolean

scala> isEven(12)
res0: Boolean = true

scala> isEven(121)
res1: Boolean = false```

What if i pass a negative value or zero to it?

```scala> isEven(0)
res2: Boolean = true

scala> isEven(-12)
res3: Boolean = true```

It works fine. 0 and -12 both are an even values.  Let’s take another function square, which takes an int value and returns square value of input.

```scala> def square(x: Int) = x * x
square: (x: Int)Int

scala> square(4)
res8: Int = 16

scala> square(12)
res9: Int = 144

scala> square(-2)
res10: Int = 4```

Let’s take one more function square root. How do we define it ?

A square root is a function which takes an integer value greater than or equal to zero and gives a value when it is multiplied by itself produces aproximately equal to same number. But what happens if an input value is negative? Then Square root is not defined because the input is invalid.

So, in other words, isEven and square both are function but the technically square root is not a function.  What is it then? Well, it is a partial function.  What does it mean by partial function?

A Partial function is a mapping or transformation from the subset of input values to output.  There is some subset of values of input where the partial function is not defined.

For example, isEven and square is defined for all values of integer but square root is defined only for positive values of integer. It is not defined for negative values.

Let’s define square root function as regular function.

```scala> def squareRoot(x: Int) = scala.math.sqrt(x)
squareRoot: (x: Int)Double

scala> squareRoot(16)
res0: Double = 4.0```

What if we provide a value zero to it ?

```scala> squareRoot(0)
res2: Double = 0.0```

It works fine. But what if we try to give invalid input?

```scala> squareRoot(-4)
res3: Double = NaN```

It gives NaN, for all invalid inputs but this is not something good way to handle this. So, first thing we should avoide defining square root as a regular function. Define it as a partial function. Partial functions allows us to query it. It would be great if we can check whether the function is defined or not for particular value.

Let’s redefine again as a partial function,

```scala> val squareRoot: PartialFunction[Int, Double] = {
| case x if x > 0 => scala.math.sqrt(x)
| }
squareRoot: PartialFunction[Int,Double] =```

We have  defined squareRoot as a partial function which takes an input and returns double value. Now thought comes to mind we could do same pattern matching in a regular function definition. But there is a difference between function and partial function. Function can only be applied but it can not be queried. In case of partial functiions, we can query it whether the input value is defined or not.

```scala> def callSquareRoot(x: Int) = {
| if(squareRoot.isDefinedAt(x)) squareRoot(x)
| }
callSquareRoot: (x: Int)AnyVal

scala> callSquareRoot(4)
res12: AnyVal = 2.0

scala> callSquareRoot(-4)
res13: AnyVal = ()```

We have queried the partial function whether it is defined for -4 or not.  This time there is no NaN value.  Empty parenthesis is there because of else part.

So, we saw what is partial function and how it can be defined. We will explore more about it in future blogs. Till then stay tuned. 😀

Please feel free to suggest and comment.  #### Written by Mahesh Chand

I am a Software Consultant with experience of more than 1.5 years. I am a Functional Programing i.e Scala and Big Data technology enthusiast.I am a active blogger, love to travel, explore and a foodie.

## 6 thoughts on “Back2Basics: Understanding Partial Functions – #13 min read”

1. Harmeet Singh(Taara) says:

Reblogged this on Harmeet Singh(Taara).

2. Anurag Srivastava says:

Reblogged this on Anurag Srivastava.

3. Mahesh Chand says:

Reblogged this on Agile Development .