Introduction
Functions with parameters let you reuse code by giving inputs to get different results. This saves time and keeps your styles neat.
@function function-name($parameter1, $parameter2) { @return calculation or value; }
@return to send back the result from the function.@function double($number) { @return $number * 2; }
@function color-brightness($color, $amount) { @return lighten($color, $amount); }
@function spacing($factor) { @return $factor * 1rem; }
@function multiply($a, $b) { @return $a * $b; } .box { width: multiply(5, 3) * 1rem; height: multiply(2, 4) * 1rem; background-color: #3498db; }
@return.@function to define and @return to send back results.@function with parameters in Sass?@function does@function defines reusable code that can take inputs and return values.double that takes one parameter $n?@function followed by the function name and parameters in parentheses.@return to send back the result.@function add($a, $b) { @return $a + $b; }
.box {
width: add(10px, 5px);
}add returns the sum of two values, here 10px + 5px = 15px.@function multiply($x, $y) {
$result: $x * $y;
}@return to send back a value; otherwise, they produce no output.responsive-size that takes a pixel value $px and returns a rem value assuming 16px base font size. Which function definition correctly achieves this?Pass the test and this pattern moves unknown