Performance: @return statement
The @return statement affects the performance of Sass compilation but does not impact browser rendering or page load speed directly.
@function calculate-padding($base) { @return $base * 1.5; } $padding: calculate-padding(10px); .element { padding: $padding; margin: $padding; border-width: $padding; }
@function calculate-padding($base) { @return $base * 1.5; } .element { padding: calculate-padding(10px); padding: calculate-padding(10px); padding: calculate-padding(10px); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using @return in Sass functions efficiently | 0 (compile-time only) | 0 | 0 | [OK] Good |
| Repeated calls to @return functions with same args | 0 (compile-time only) | 0 | 0 | [!] Warning |
@return statement do in Sass?@return@return statement is used inside a Sass function to send a value back to where the function was called.@return.@return sends value back [OK]@return always sends a value back [OK]@return creates CSS rules@return with variable declaration@return outside a functiondouble that doubles a number $n?@function and use @return to send back a value.@return correctly. @function double($n) { return $n * 2; } misses the @ before return. @function double($n) { $n * 2; } lacks any return statement. @function double($n) { @output $n * 2; } uses an invalid @output keyword.@return inside @function [OK]@return with @function [OK]@function triple($x) {
@return $x * 3;
}
.box {
width: triple(10px);
}triple multiplies the input by 3 and returns the result.triple(10px) returns 10px * 3 = 30px. This value is assigned to the width property.@function add($a, $b) {
$sum: $a + $b;
}$sum but never returns it. Sass functions must use @return to send a value back.@return statement to send back the result. -> Option C@return [OK]@return [OK]adjust-color-brightness that takes a color and a percentage, then returns the color lightened or darkened by that percentage. Which of these is the best way to use @return inside this function?$percent. Using @if with separate @return statements is clear and correct.return without @, which is invalid Sass syntax. @function adjust-color-brightness($color, $percent) {
lighten($color, $percent);
darken($color, abs($percent));
} lacks any @return, so no value is returned. @function adjust-color-brightness($color, $percent) {
@return lighten($color, $percent);
@return darken($color, abs($percent));
} has two @return statements in sequence, which is invalid because only one @return can be executed.@return for multiple outcomes [OK]@return per condition inside functions [OK]@return statements without conditionsPass the test and this pattern moves unknown