What if you could fix a color change in one place and see it everywhere instantly?
Why Function definition with @function in SASS? - Purpose & Use Cases
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.
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.
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.
color: darken(#3498db, 20%); color: darken(#3498db, 20%);
@function darken20($color) {
@return darken($color, 20%);
}
color: darken20(#3498db);
color: darken20(#3498db);You can create reusable, easy-to-update style calculations that keep your code clean and consistent.
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.
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.
Practice
@function directive do in Sass?Solution
Step 1: Understand the purpose of
The@function@functiondirective in Sass is used to create reusable blocks of code that can return a value.Step 2: Compare with other options
Options A, B, and D describe other Sass or CSS features, but only C correctly describes@function.Final Answer:
It defines reusable code blocks that return values. -> Option AQuick Check:
@functioncreates reusable code blocks [OK]
- Confusing @function with @mixin
- Thinking @function applies styles
- Mixing up @function with variable declaration
double that returns twice the input $n in Sass?Solution
Step 1: Check the function declaration syntax
In Sass, functions start with@function, followed by the name and parameters in parentheses.Step 2: Verify the return statement
The correct way to return a value is using@return, not justreturn.Final Answer:
@function double($n) { @return $n * 2; } -> Option AQuick Check:
Use@functionand@returnfor Sass functions [OK]
- Using 'return' instead of '@return'
- Omitting the @ in @function
- Confusing @function with @mixin
@function add($a, $b) { @return $a + $b; }What will be the output of this Sass code?
.result { width: add(10px, 5px); }Solution
Step 1: Understand the function behavior
The functionaddtakes two values and returns their sum using@return $a + $b;.Step 2: Apply the function in the style
Callingadd(10px, 5px)returns15px, so the CSS becomes.result { width: 15px; }.Final Answer:
.result { width: 15px; } -> Option BQuick Check:
Function returns sum, so width is 15px [OK]
- Expecting function name to appear in output
- Confusing list output with addition
- Thinking it causes syntax error
@function multiply($x, $y) { return $x * $y; }Solution
Step 1: Check the return statement syntax
In Sass functions, the return keyword must be@return, not justreturn.Step 2: Verify other parts
The function name and parameters are valid, and multiple parameters are allowed.Final Answer:
Missing @ before return keyword -> Option CQuick Check:
Use@return, notreturn[OK]
- Using 'return' without @
- Thinking parameters must be strings
- Believing function names can't have multiple parameters
shade-color that darkens a color by a percentage. Which of these function definitions correctly uses @function and @return to achieve this?A) @function shade-color($color, $percent) { @return darken($color, $percent); }
B) @function shade-color($color, $percent) { return darken($color, $percent); }
C) @mixin shade-color($color, $percent) { @return darken($color, $percent); }
D) @function shade-color($color, $percent) { darken($color, $percent); }Solution
Step 1: Confirm correct function syntax
The function must start with@functionand use@returnto send back the value.Step 2: Check each option
@function shade-color($color, $percent) { @return darken($color, $percent); } uses@functionand@returncorrectly. @function shade-color($color, $percent) { return darken($color, $percent); } usesreturnwithout @, which is invalid. @mixin shade-color($color, $percent) { @return darken($color, $percent); } uses@mixininstead of@function. @function shade-color($color, $percent) { darken($color, $percent); } lacks a return statement.Final Answer:
@function shade-color($color, $percent) { @return darken($color, $percent); } -> Option DQuick Check:
Use @function with @return for value-returning functions [OK]
- Using return without @
- Confusing mixin with function
- Omitting @return statement
Ready to prove it?
Pass the test and this pattern moves unknown
