A function with parameters in Sass is a reusable block of code that takes inputs (parameters) to perform calculations or return values based on those inputs.
Click to reveal answer
beginner
How do you define a function with parameters in Sass?
Use <code>@function</code> followed by the function name and parentheses containing parameters. Example: <code>@function double($number) { @return $number * 2; }</code>
Click to reveal answer
beginner
Why use parameters in Sass functions?
Parameters let you pass different values to the function each time you use it, making your code flexible and reusable.
Click to reveal answer
beginner
What does this Sass function do?<br><pre>@function add($a, $b) { @return $a + $b; }</pre>
It takes two numbers, $a and $b, and returns their sum.
Click to reveal answer
beginner
How do you call a Sass function with parameters?
Use the function name followed by parentheses with arguments. Example: width: add(10px, 5px); calls the add function with 10px and 5px.
Click to reveal answer
How do you define a function with parameters in Sass?
Afunction name($param) { ... }
B@mixin name($param) { ... }
Cdef name($param) { ... }
D@function name($param) { ... }
✗ Incorrect
In Sass, functions are defined using @function followed by the name and parameters in parentheses.
What does the parameter in a Sass function do?
AImports another Sass file
BStores a value passed to the function
CCreates a new CSS selector
DDefines a CSS property
✗ Incorrect
Parameters hold values passed into the function to be used inside it.
What keyword returns a value from a Sass function?
Areturn
Byield
C@return
Doutput
✗ Incorrect
Sass uses @return to send back a value from a function.
How do you call a function named 'multiply' with parameters 3 and 4 in Sass?
Amultiply(3, 4)
B@include multiply(3, 4)
Cmultiply 3, 4
D@function multiply(3, 4)
✗ Incorrect
Functions are called by their name with arguments in parentheses.
Which of these is NOT true about Sass functions with parameters?
AThey create CSS selectors
BThey can return values
CThey can take multiple parameters
DThey help reuse code
✗ Incorrect
Functions do not create selectors; they return values for use in styles.
Explain how to create and use a function with parameters in Sass.
Think about how you pass ingredients to a recipe and get a dish back.
You got /4 concepts.
Why are parameters useful in Sass functions? Give an example.
Imagine a calculator that can add any two numbers you give it.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using @function with parameters in Sass?
easy
A. To define CSS classes with fixed styles
B. To import external CSS files
C. To create reusable code blocks that accept different inputs
D. To add comments inside Sass files
Solution
Step 1: Understand what @function does
@function defines 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 C
Quick Check:
Functions with parameters = reusable code with inputs [OK]
Hint: Functions reuse code by accepting inputs [OK]
Common Mistakes:
Confusing functions with mixins
Thinking functions define CSS classes
Assuming functions import files
2. Which of the following is the correct syntax to define a Sass function named double that takes one parameter $n?
easy
A. @function double { $n -> $n * 2; }
B. function double($n) { return $n * 2; }
C. @mixin double($n) { @return $n * 2; }
D. @function double($n) { @return $n * 2; }
Solution
Step 1: Check the function keyword and parameter syntax
The correct keyword is @function followed by the function name and parameters in parentheses.
Step 2: Verify the return statement
The function must use @return to send back the result.
The function add returns 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 A
Quick Check:
Function returns sum, output uses computed value [OK]
Hint: Functions return values replaced in CSS output [OK]
Common Mistakes:
Expecting function name in output
Thinking Sass outputs raw expressions
Confusing mixin behavior with functions
4. Identify the error in this Sass function:
@function multiply($x, $y) {
$result: $x * $y;
}
medium
A. Incorrect variable declaration syntax
B. Missing @return statement to output the result
C. Function name should be @mixin
D. Parameters must be declared without $
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 @return to send back a value; otherwise, they produce no output.
Final Answer:
Missing @return statement to output the result -> Option B
Quick Check:
Functions need @return to output values [OK]
Hint: Always use @return to output function results [OK]
Common Mistakes:
Forgetting @return in functions
Confusing mixins with functions
Using variables without $
5. You want a Sass function responsive-size that takes a pixel value $px and returns a rem value assuming 16px base font size. Which function definition correctly achieves this?
B. @function responsive-size($px) { @return $px * 16rem; }
C. @function responsive-size($px) { @return $px + 16rem; }
D. @function responsive-size($px) { @return rem($px / 16); }
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.