Discover how a single line can save you hours of tedious style updates!
Why @return statement in SASS? - Purpose & Use Cases
Imagine you write a style that calculates a color shade by hand every time you need it. You copy and paste the same color math in many places.
If you want to change the shade formula, you must find and update every copy manually. This wastes time and risks mistakes or inconsistent colors.
The @return statement lets you create reusable functions that calculate and send back values. Change the function once, and all uses update automatically.
$shade1: lighten(#333, 10%); $shade2: lighten(#333, 10%); $shade3: lighten(#333, 10%);
@function shade($color) {
@return lighten($color, 10%);
}
$shade1: shade(#333);
$shade2: shade(#333);
$shade3: shade(#333);You can write clean, DRY styles that adapt easily by returning values from functions with @return.
When theming a website, you can create a function that returns a color based on a base color. Changing the base updates all related colors instantly.
Manually repeating calculations is slow and error-prone.
@return lets functions send back values to reuse.
This makes styles easier to maintain and update.
Practice
@return statement do in Sass?Solution
Step 1: Understand the purpose of
The@return@returnstatement is used inside a Sass function to send a value back to where the function was called.Step 2: Differentiate from other Sass features
Other options describe different Sass features: creating CSS rules, importing files, or defining variables, which are not the role of@return.Final Answer:
It sends a value back from a Sass function. -> Option AQuick Check:
@returnsends value back [OK]
@return always sends a value back [OK]- Thinking
@returncreates CSS rules - Confusing
@returnwith variable declaration - Using
@returnoutside a function
double that doubles a number $n?Solution
Step 1: Identify correct Sass function syntax
In Sass, functions start with@functionand use@returnto send back a value.Step 2: Check each option for correct usage
@function double($n) { @return $n * 2; } uses@returncorrectly. @function double($n) { return $n * 2; } misses the@beforereturn. @function double($n) { $n * 2; } lacks any return statement. @function double($n) { @output $n * 2; } uses an invalid@outputkeyword.Final Answer:
@function double($n) { @return $n * 2; } -> Option DQuick Check:
Use@returninside@function[OK]
@return with @function [OK]- Omitting the '@' before return
- Forgetting to include a return statement
- Using invalid keywords like '@output'
@function triple($x) {
@return $x * 3;
}
.box {
width: triple(10px);
}Solution
Step 1: Understand the function behavior
The functiontriplemultiplies the input by 3 and returns the result.Step 2: Calculate the returned value
Callingtriple(10px)returns10px * 3 = 30px. This value is assigned to the width property.Final Answer:
.box { width: 30px; } -> Option AQuick Check:
Function returns 3 times input [OK]
- Expecting the function name to appear in CSS
- Ignoring units like px in calculations
- Assuming no multiplication happens
@function add($a, $b) {
$sum: $a + $b;
}Solution
Step 1: Check function body for return statement
The function calculates$sumbut never returns it. Sass functions must use@returnto send a value back.Step 2: Verify other syntax elements
Variable declaration and addition are correct. Function names do not start with a dollar sign. Addition is allowed inside functions.Final Answer:
Missing@returnstatement to send back the result. -> Option CQuick Check:
Functions need@return[OK]
@return [OK]- Forgetting to return the calculated value
- Thinking function names need $ prefix
- Assuming calculations auto-return
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?Solution
Step 1: Understand conditional return usage
The function should return either a lightened or darkened color based on the sign of$percent. Using@ifwith separate@returnstatements is clear and correct.Step 2: Evaluate other options
@function adjust-color-brightness($color, $percent) { return if($percent > 0, lighten($color, $percent), darken($color, abs($percent))); } usesreturnwithout@, 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@returnstatements in sequence, which is invalid because only one@returncan be executed.Final Answer:
@function adjust-color-brightness($color, $percent) { @if $percent > 0 { @return lighten($color, $percent); } @else { @return darken($color, abs($percent)); } } -> Option BQuick Check:
Use conditional@returnfor multiple outcomes [OK]
@return per condition inside functions [OK]- Placing multiple
@returnstatements without conditions - Not returning any value
- Using function calls without returning them
Ready to prove it?
Pass the test and this pattern moves unknown
