Performance: Function definition with @function
This affects the CSS compilation time and the final CSS file size, impacting page load speed.
@function color-shade($color, $percent) { @return lighten($color, $percent); } .element1 { color: color-shade(#333, 20%); } .element2 { color: color-shade(#333, 20%); }
@mixin color-shade($color, $percent) { color: lighten($color, $percent); } .element1 { @include color-shade(#333, 20%); } .element2 { @include color-shade(#333, 20%); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using @mixin for repeated styles | No direct DOM impact | No reflows | Larger CSS causes slower paint | [X] Bad |
| Using @function to return values | No direct DOM impact | No reflows | Smaller CSS improves paint speed | [OK] Good |
@function directive do in Sass?@function@function directive in Sass is used to create reusable blocks of code that can return a value.@function.@function creates reusable code blocks [OK]double that returns twice the input $n in Sass?@function, followed by the name and parameters in parentheses.@return, not just return.@function and @return for Sass functions [OK]@function add($a, $b) { @return $a + $b; }.result { width: add(10px, 5px); }add takes two values and returns their sum using @return $a + $b;.add(10px, 5px) returns 15px, so the CSS becomes .result { width: 15px; }.@function multiply($x, $y) { return $x * $y; }@return, not just return.@return, not return [OK]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); }@function and use @return to send back the value.@function and @return correctly. @function shade-color($color, $percent) { return darken($color, $percent); } uses return without @, which is invalid. @mixin shade-color($color, $percent) { @return darken($color, $percent); } uses @mixin instead of @function. @function shade-color($color, $percent) { darken($color, $percent); } lacks a return statement.Pass the test and this pattern moves unknown