0
0
SASSmarkup~3 mins

Why Function definition with @function in SASS? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could fix a color change in one place and see it everywhere instantly?

The Scenario

Imagine you want to reuse a color calculation in many places in your stylesheet. You write the same math over and over, like darkening a color by 20% each time.

The Problem

Copying and pasting the same calculation everywhere is slow and risky. If you want to change the darkening amount, you must find and update every single place manually. This wastes time and causes mistakes.

The Solution

Using @function lets you write the calculation once with a name. Then you call it anywhere in your styles. Change the function once, and all uses update automatically.

Before vs After
Before
color: darken(#3498db, 20%);
color: darken(#3498db, 20%);
After
@function darken20($color) {
  @return darken($color, 20%);
}

color: darken20(#3498db);
color: darken20(#3498db);
What It Enables

You can create reusable, easy-to-update style calculations that keep your code clean and consistent.

Real Life Example

A design system where button colors need consistent shading. Define a darken function once, then use it for all buttons to keep colors uniform and easy to tweak.

Key Takeaways

Writing repeated calculations manually wastes time and causes errors.

@function lets you define reusable style calculations.

Updating the function updates all uses, saving effort and ensuring consistency.