2021-09-22

|~2 min read

|359 words

The domain of a function is the set of inputs for which a function is defined.

The range of a function is the set of values that are mapped to the function’s domain.

Total functions have a few attributes related to domains and ranges:

- For every value in the domain, there’s a value in the range.
- For every value in the domain, there is a unique match in the range.

Let’s look at some examples:

add

`const add = (x: number, y: number): number => x + y`

The `add`

function’s domain is all numbers and that maps to a range which includes all numbers.

This variation of add is total.

On the other hand, we could have `addSome`

:

addSome

`const addSome = (x: 1 | 2 | 3, y: 1 | 2 | 3): 2 | 3 | 4 | 5 | 6 => x + y`

This is *also* a total function. Every member of the domain maps to a value in the range. It could also be written as:

addSome

`const addSome = (x: 1 | 2 | 3, y: 1 | 2 | 3): number => x + y`

But what about:

addPartial

```
const addSome = (x: number, y: number): number => {
if (1 <= x && x <= 3) {
return x + y
}
}
```

This is partial because the domain is any number, but there are values within the domain for which there’s no return value.

The other attribute of a total function is that the domain maps to a unique value in the range.

This means that every time you use a set of inputs, you get the same output.

In other words, this won’t qualify as a function:

random

`const random = (x: number): number => (Math.random() ? 1 : 2)`

This is not meant to be an exhaustive review of domains, ranges, and set theory, but a primer. Hopefully it helps clarify some ideas!

If you’d like more, MathIsFun.com has a good write up on Domains, Ranges, and Codomains which is worth a look.

Related Posts

Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!