Chepter 9: Sass Functions

Sass Functions — the part where Sass starts feeling like a real programming language inside your stylesheets.

We’re going to treat this like a hands-on coding session: first understand built-ins (what Sass gives you for free), then learn how to write your own custom @functions, with lots of practical examples, arguments, returns, control flow, and 2026 modern tips (Dart Sass reality, module system, deprecations).

1. What Are Sass Functions? (The Big Picture)

Sass functions are reusable pieces of logic that take input(s), do calculations / transformations, and return a single value.

  • They compute things (colors, sizes, strings, lists…)
  • They return values (not output CSS rules like mixins do)
  • You call them like normal SassScript expressions: darken($color, 15%)
  • Built-ins are in modules like sass:color, sass:math, sass:string, sass:list, sass:map…
  • You can write your own with @function

Key difference from mixins:

Feature Functions (@function) Mixins (@mixin)
Purpose Compute & return one value Output CSS declarations
Called with In expressions: func($arg) @include func();
Can have @return Yes (required) No (optional with @content)
Use case Color math, string manipulation, utils Reusable style blocks, parametric CSS

Functions → values Mixins → styles

2. Built-in Functions – The Ones You Use Every Day (2026 Module Style)

In modern Dart Sass (1.95+ as of Feb 2026), load modules explicitly with @use ‘sass:…’; — globals are deprecated for most.

Common modules & favorites:

SCSS

Examples:

SCSS

Pro tip 2026:

  • Use color.adjust() / color.scale() instead of old darken(), lighten() (deprecated in favor of CSS Color 4 compat since ~1.79)
  • math.div() for division (old / can be ambiguous now)
  • if() Sass function is deprecated in 1.95+ → use ternary: condition ? true-value : false-value

3. Writing Your Own Custom Functions – @function Syntax

Syntax:

SCSS

Must have @return — otherwise error!

Example 1: Simple spacing calculator

SCSS

Example 2: Ratio-based font size (modular scale)

SCSS

Example 3: Strip unit (very common utility)

SCSS

Example 4: Advanced – Color contrast checker (simple version)

SCSS

Example 5: Argument list (…) for flexible inputs

SCSS

4. Modern 2026 Best Practices & Gotchas

  • Put custom functions in _functions.scss or _utils.scss
  • Load with @use ‘functions’ as f; → call f.spacing(2)
  • Or @use ‘functions’ as *; for globals (convenient but less traceable)
  • Use keyword arguments when >2–3 params
  • Return typed values (color, number, string…) — helps debugging
  • Avoid side effects — functions should be pure (same input → same output)
  • For higher-order functions: use meta.get-function() + meta.call()
SCSS
  • Deprecations to watch: if() function → use ternary instead
  • No direct way to return multiple values → return list/map

5. Quick Challenge – Write This!

Create a function that returns a fluid font size using clamp():

SCSS

Compile → see beautiful responsive typography!

You’ve now unlocked Sass functions — this is where you can build powerful design tokens, utilities, and theme logic.

Next — want to combine functions with maps for full theme switching? Or build a complete typography scale system with custom functions? Or explore sass:meta for advanced tricks?

Just tell me — we’re deep in Sass territory now! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *