The Sass @function rule defines reusable operations on SassScript values. Write @function name($args…) { … }, and finish with @return. Call functions with normal CSS function syntax.
Names can be any Sass identifier that does not begin with --. Prefer functions for pure computation; use mixins when you need to emit CSS or other side effects.
Official pattern.
Per the Sass docs: abstract common formulas into readable helpers, then use the returned value in property declarations.
Hyphens = underscores.
Like other Sass identifiers, scale-color and scale_color refer to the same function.
No side effects.
Technically a function can set globals, but that is strongly discouraged. Keep functions for values; put side effects in mixins.
meta.inspect prints the string into a custom property for the demo.
Edge Cases & Pitfalls
Most @function bugs are missing returns, side effects, or typos that become plain CSS.
return
Every path must @return
A function alone emits no CSS. Every branch needs a returned value, including early exits.
mixin
Need styles? Use a mixin
Functions compute values. Emitting rules, nested selectors, or globals belongs in @mixin.
typo
Unknown names become CSS
Misspelled helpers often compile as plain CSS functions. Lint output or prefer namespaced module APIs.
rename
Keyword args are part of the API
Renaming a public parameter can break callers. Keep the old name temporarily and @warn during migration.
Toolchain
Sass Support
@function is a core Sass at-rule. Browsers never see it — only the values your functions return.
Implementation
Status
Dart Sass
Supported (recommended)
LibSass / node-sass
Supported (deprecated toolchain)
Ruby Sass
Supported (end-of-life)
Browsers
N/A — only see returned CSS values
Prefer Dart Sass for new projects. SCSS and indented syntax both support @function.
Remember
Key Takeaways
Compute:@function returns a value; call it like CSS.
Always @return — early returns are encouraged for edge cases.
Flexible APIs: defaults, keywords, and $args....
Pure values only — emit CSS with mixins, not functions.
One line:@function packages reusable calculations you call like CSS and resolve at compile time.
Compile
Compile Notes
@function never reaches the browser. Conceptual flow:
flow.txt
1. Define @function with arguments
2. Call it in a property value or expression
3. Evaluate the body until @return
4. Substitute the returned SassScript value
5. Emit CSS — browsers never see the function name
Frequently Asked Questions
A @function defines a reusable SassScript calculation. You call it with normal CSS function syntax, and it must end by returning a value with @return.
Functions compute and return values used in property values or other expressions. Mixins (@mixin / @include) insert styles (and can use @content). Use mixins for side effects; use functions just to compute values.
Yes. @return is only allowed inside @function, and each function must return a value. When Sass hits @return, the function ends immediately—useful for early exits.
Yes. Give defaults with $name: value, and call with keywords like rem(8px, $base: 16px). The last parameter can end in ... to accept an argument list.
Unknown calls compile to plain CSS functions (unless they use Sass-only argument syntax). That makes typos easy to miss—lint your CSS output or stick to namespaced module APIs.
Yes. Like other Sass identifiers, scale-color and scale_color refer to the same function.