Matchbox 0.2

clamp

Restrict an integer to an inclusive minimum and maximum range.

Signature

func clamp(num int, min int, max int) int

Parameters

  • num — the integer to restrict.
  • min — the lowest permitted result.
  • max — the highest permitted result.

Return Value

The intended result is:

  • num when it is already within the range;
  • min when num is less than min;
  • max when num is greater than max.

Example

var volume = clamp(120, 0, 100)
print(volume)

The example writes 100 to standard output.