What if you could change all your button styles by updating just one place?
Why Functions with parameters in SASS? - Purpose & Use Cases
Imagine you are styling buttons on a website. You want each button to have a different color and size, so you write separate styles for each one manually.
If you want to change the button style later, you must update every single style manually. This takes a lot of time and can cause mistakes or inconsistencies.
Functions with parameters let you create one reusable style that changes based on the input you give. You write the style once and just pass different values to get different results.
$btn-red-color: #f00; $btn-red-size: 1.2rem; .button-red { color: $btn-red-color; font-size: $btn-red-size; } .button-blue { color: #00f; font-size: 1rem; }
@mixin button-style($color, $size) {
color: $color;
font-size: $size;
}
.button-red {
@include button-style(#f00, 1.2rem);
}
.button-blue {
@include button-style(#00f, 1rem);
}You can create flexible, reusable styles that adapt easily by just changing the input values.
Think about a website with many buttons: primary, secondary, disabled. Instead of writing separate styles for each, you write one function and pass different colors and sizes to style them all consistently.
Manual styling for each variation is slow and error-prone.
Functions with parameters let you reuse code with different inputs.
This makes your styles easier to maintain and update.
Practice
@function with parameters in Sass?Solution
Step 1: Understand what
@functiondoes@functiondefines reusable code that can take inputs and return values.Step 2: Identify the role of parameters
Parameters allow the function to work with different values each time it is called.Final Answer:
To create reusable code blocks that accept different inputs -> Option CQuick Check:
Functions with parameters = reusable code with inputs [OK]
- Confusing functions with mixins
- Thinking functions define CSS classes
- Assuming functions import files
double that takes one parameter $n?Solution
Step 1: Check the function keyword and parameter syntax
The correct keyword is@functionfollowed by the function name and parameters in parentheses.Step 2: Verify the return statement
The function must use@returnto send back the result.Final Answer:
@function double($n) { @return $n * 2; } -> Option DQuick Check:
Correct function syntax uses @function and @return [OK]
- Using 'function' without @
- Mixing up mixins and functions
- Omitting @return keyword
@function add($a, $b) { @return $a + $b; }
.box {
width: add(10px, 5px);
}Solution
Step 1: Understand the function call
The functionaddreturns the sum of two values, here 10px + 5px = 15px.Step 2: Check the output CSS
The Sass compiler replaces the function call with the returned value, so width becomes 15px.Final Answer:
.box { width: 15px; } -> Option AQuick Check:
Function returns sum, output uses computed value [OK]
- Expecting function name in output
- Thinking Sass outputs raw expressions
- Confusing mixin behavior with functions
@function multiply($x, $y) {
$result: $x * $y;
}Solution
Step 1: Check function body for return
The function assigns the product to a variable but does not return it.Step 2: Understand function requirements
Sass functions must use@returnto send back a value; otherwise, they produce no output.Final Answer:
Missing @return statement to output the result -> Option BQuick Check:
Functions need @return to output values [OK]
- Forgetting @return in functions
- Confusing mixins with functions
- Using variables without $
responsive-size that takes a pixel value $px and returns a rem value assuming 16px base font size. Which function definition correctly achieves this?Solution
Step 1: Understand rem conversion
To convert pixels to rem, divide the pixel value by the base font size (16).Step 2: Write the correct return expression
Multiply the division result by 1rem to get the rem unit value.Step 3: Check each option
@function responsive-size($px) { @return $px / 16 * 1rem; } correctly divides $px by 16 and multiplies by 1rem. Others either multiply incorrectly or use invalid functions.Final Answer:
@function responsive-size($px) { @return $px / 16 * 1rem; } -> Option AQuick Check:
Divide px by 16, multiply by 1rem for rem units [OK]
- Multiplying px by 16 instead of dividing
- Adding units instead of multiplying
- Using undefined rem() function
Ready to prove it?
Pass the test and this pattern moves unknown
