TExcelTAD
Home › Sass › Sass @function

Sass @function

A function computes and returns a single value. Where a mixin outputs declarations, a function outputs a value that you use inside a declaration — like rem(24px) or spacing(3). Define one with @function and return a result with @return.

scss
@function name($arguments) {
  @return <value>;
}

Your first function

scss · try it yourself
@use "sass:math";

@function rem($px, $root: 16px) {
  @return math.div($px, $root) * 1rem;
}

html { font-size: 100%; }
h2 { font-size: rem(28px); margin-bottom: rem(12px); }
.small { font-size: rem(14px); }
html {
  font-size: 100%;
}

h2 {
  font-size: 1.75rem;
  margin-bottom: 0.75rem;
}

.small {
  font-size: 0.875rem;
}

Functions are called by name, with arguments in parentheses, anywhere a value is expected.

Arguments

Functions take the same kinds of arguments as mixins:

Feature Example
Default value @function rem($px, $root: 16px)
Keyword call rem(24px, $root: 10px)
Variable arguments @function sum($numbers...)
scss · try it yourself
@function sum($numbers...) {
  $total: 0;
  @each $n in $numbers {
    $total: $total + $n;
  }
  @return $total;
}

.toolbar {
  height: sum(8px, 24px, 8px);
  width: sum(120px, 16px, 120px, 16px, 120px);
}
.toolbar {
  height: 40px;
  width: 392px;
}

A spacing system function

Functions make design tokens easy to use consistently:

scss · try it yourself
$space-unit: 4px;

@function space($steps) {
  @if $steps < 0 {
    @error "space() expects a positive number, got #{$steps}";
  }
  @return $steps * $space-unit;
}

.card {
  padding: space(4) space(5);
  margin-bottom: space(6);
  gap: space(2);
}
.card {
  padding: 16px 20px;
  margin-bottom: 24px;
  gap: 8px;
}

@error stops compilation with a helpful message if someone passes a nonsense value — you'll learn more in the @debug, @warn & @error lesson.

Functions with logic

A function can contain variables, @if, loops — anything except output CSS. Here a function returns the right contrast colour for a background:

scss · try it yourself
@use "sass:color";

@function contrast($bg, $light: white, $dark: #222) {
  $l: color.channel($bg, "lightness", $space: hsl);
  @return if-dark($l, $light, $dark);
}

@function if-dark($lightness, $light, $dark) {
  @if $lightness < 55% {
    @return $light;
  }
  @return $dark;
}

.tag-plum   { background: #6a1b9a; color: contrast(#6a1b9a); }
.tag-butter { background: #fff59d; color: contrast(#fff59d); }
.tag-plum {
  background: #6a1b9a;
  color: white;
}

.tag-butter {
  background: #fff59d;
  color: #222;
}

Notice that a function can call another function, even one defined later in the file.

Mixin or function?

Use a… When you want to produce… Example
@function One value width: col-width(3);
@mixin Declarations or rules @include truncate;

Rules and tips

  • A function must reach an @return; a function that ends without returning is an error.
  • Functions can't contain declarations or style rules.
  • Avoid names that clash with CSS functions such as calc, min, max, clamp or var.
  • Prefer a namespaced built-in (math.div, color.adjust) to writing your own when one exists.

Tip: Put shared functions in a partial like _functions.scss and load them with @use "functions" as fn; → fn.rem(24px).

Test yourself

What keyword sends a value back from a Sass function?

  1. @return
  2. return
  3. @output
  4. @yield

Which statement is true?

  1. Functions can output CSS rules
  2. Mixins return a single value
  3. Functions return a value; mixins output declarations
  4. They are interchangeable

What does space(3) return if $space-unit is 4px and space() multiplies by it?

  1. 3px
  2. 7px
  3. 12px
  4. 34px