# Scala Tutorial - Learn Function Composition Using AndThen

## Overview

In this tutorial, we will learn how to create value functions which are defined using the **val** keyword as opposed to using the **def** keyword.

Val functions inherit the **andThen** function and we will show how to use the **andThen** function to compose two functions together.

Mathematically speaking, **f(x) andThen g(x) = g(f(x))**. The results of the first function f(x) is ran first and will be passed as input to the second function g(x).

## Steps

**1. Assume a pre-calculated total cost amount**

Let's start with a simple **totalCost** value which represents the total cost in dollar figure for a particular customer buying donuts from your store.

```
println("Step 1: Assume a pre-calculated total cost amount")
val totalCost: Double = 10
```

**2. How to define a val function to apply discount to total cost**

Similar to the example from the previous tutorial on defining function with val keyword, let's define a val function which will apply some discount dollar value from the total cost figure.

```
println("\nStep 2: How to define a val function to apply discount to total cost")
val applyDiscountValFunction = (amount: Double) => {
println("Apply discount function")
val discount = 2 // fetch discount from database
amount - discount
}
```

**3. How to call a val function**

Calling the val function applyDiscountValFunction from Step 2 is very straight-forward. You simply need to pass it the totalCost value from Step 1.

```
println("\nStep 3: How to call a val function")
println(s"Total cost of 5 donuts with discount = ${applyDiscountValFunction(totalCost)}")
```

You should see the following output when you run your Scala application in IntelliJ:

```
Step 3: How to call a val function
Apply discount function
Total cost of 5 donuts with discount = 8.0
```

**4. How to define a val function to apply tax to total cost**

Let's go ahead and define another val function which should apply some tax amount to the totalCost value.

```
println("\nStep 4: How to define a val function to apply tax to total cost")
val applyTaxValFunction = (amount: Double) => {
println("Apply tax function")
val tax = 1 // fetch tax from database
amount + tax
}
```

**5. How to call andThen on a val function**

As we've seen from the previous tutorial on defining function with val keyword, val function inherits an **andThen** function.

Calling **andThen** will take the result from the first function and pass it as input parameter to the second function. Let's use the **andThen** semantics to apply discount andThen apply tax to the totalCost figure as shown below.

```
println("\nStep 5: How to call andThen on a val function")
println(s"Total cost of 5 donuts = ${ (applyDiscountValFunction andThen applyTaxValFunction)(totalCost) }")
```

You should see the following output when you run your Scala application in IntelliJ:

```
Step 5: How to call andThen on a val function
Apply discount function
Apply tax function
Total cost of 5 donuts = 9.0
```

**NOTE:**

- The apply discount function was called first andThen the apply tax function was called.
- The output from the first apply discount function was also passed through as input parameter to the second apply tax function

## Summary

In this tutorial, we went over the following:

- How to define a value of type Double to represent total cost
- How to define a val function to apply discount to total cost
- How to call a val function
- How to define a value function to apply tax to total cost
- How to call andThen on a val function

## Tip

**val**functions are instances of**FunctionN**classes and if you look at the Scala documentation for say Function1 class, you will notice that**val**function will inherit other methods such as**andThen**or**compose**which allow for**function composition**.

## Source Code

The source code is available on the allaboutscala GitHub repository.

## What's Next

In the next tutorial, I will show you how to use the **compose** function inherited when you define **val** function.

**Share this article on**