# Mod Function

A mod function divides two numbers and returns the remainder. It requires the same input values as a division operation but outputs the "leftover" value instead of the division result. For example:

- 7 mod 3 = 1
- 8 mod 3 = 2

If there is no remainder, the result is zero.

- 9 mod 3 = 0

In mathematical formulas, the mod or "modulo" operator is often represented with a percentage sign. For example, 7 mod 3 can be written as the following expression:

- 7 % 3 = 1

In computer science, a mod function may be written as **mod(x,y)**, or something similar, depending on the programming language. For example:

- mod(7,3) = 1

The function above divides the first value (dividend) by the second value (divisor) and returns the remainder. Because 7 ÷ 3 = 2r1, the result of mod(7,3) is 1.

### Mod Function Applications

The mod function has several uses in computer programming. Since the result must be less than the divisor, it can limit results to a specific range. For example **mod(x,4)** can only return 0, 1, 2, or 3, assuming x is an integer. It is useful for checking every *nth* value or categorizing results into a limited number of "buckets."

In most programming languages, 0 evaluates as false, and other numbers evaluate as true. Therefore, a mod function can provide a boolean result — false if two numbers are evenly divisible and true if they are not. Below is an example of a mod function within an if statement:

if (mod(x,5)) then { ... }

If x is not evenly divisible by 5, the code in the then clause will execute. If x is divisible by five — 5, 10, 15, 20, etc — the code will not.

**NOTE:** Mod functions often have two integers as arguments. However, floating point numbers are also acceptable. Two integer values always produce an integer, but one or more fractional inputs may output a fractional result. For instance, 8 % 2.5 = 0.5 since 8 ÷ 2.5 = 3r0.5.