📖 Scenario: You are styling a website and want to create a reusable Sass function that calculates the double of a number. This will help you keep your styles consistent and easy to update.
🎯 Goal: Build a Sass function named double that takes one number as input and returns its double. Then use this function to set the width of a container.
📋 What You'll Learn
Create a Sass function named double using @function
The function must take one parameter named $number
The function must return $number * 2
Use the double function to set the width of a .container class to double 100px
💡 Why This Matters
🌍 Real World
Sass functions help you write reusable and maintainable styles by calculating values dynamically.
💼 Career
Knowing how to write Sass functions is useful for front-end developers to create scalable CSS codebases.
Progress0 / 4 steps
1
Create the Sass function skeleton
Write a Sass function named double using @function that takes one parameter called $number. Do not add the return value yet.
SASS
Hint
Use @function followed by the function name and parameters in parentheses. Inside, use @return to send back a value.
2
Add the return statement to double the number
Modify the double function to return the value of $number * 2 instead of null.
SASS
Hint
Replace null with $number * 2 in the @return statement.
3
Use the function to set container width
Create a CSS class called .container and set its width property to the result of calling the double function with 100px as the argument.
SASS
Hint
Use the function by writing double(100px) as the value for width.
4
Add a comment explaining the function
Add a comment above the @function double line explaining that this function doubles a number.
SASS
Hint
Write a single-line comment starting with // describing the function.
Practice
(1/5)
1. What does the @function directive do in Sass?
easy
A. It defines reusable code blocks that return values.
B. It imports external CSS files.
C. It creates variables for colors.
D. It applies styles to HTML elements.
Solution
Step 1: Understand the purpose of @function
The @function directive in Sass is used to create reusable blocks of code that can return a value.
Step 2: Compare with other options
Options A, B, and D describe other Sass or CSS features, but only C correctly describes @function.
Final Answer:
It defines reusable code blocks that return values. -> Option A
Quick Check:
@function creates reusable code blocks [OK]
Hint: Remember: @function returns values, not styles directly [OK]
Common Mistakes:
Confusing @function with @mixin
Thinking @function applies styles
Mixing up @function with variable declaration
2. Which of the following is the correct syntax to define a function named double that returns twice the input $n in Sass?
easy
A. @function double($n) { @return $n * 2; }
B. @function double($n) { return $n * 2; }
C. function double($n) { @return $n * 2; }
D. @mixin double($n) { @return $n * 2; }
Solution
Step 1: Check the function declaration syntax
In Sass, functions start with @function, followed by the name and parameters in parentheses.
Step 2: Verify the return statement
The correct way to return a value is using @return, not just return.
Hint: Use @function and @return keywords exactly [OK]
Common Mistakes:
Using 'return' instead of '@return'
Omitting the @ in @function
Confusing @function with @mixin
3. Given the Sass function:
@function add($a, $b) { @return $a + $b; }
What will be the output of this Sass code?
.result { width: add(10px, 5px); }
medium
A. .result { width: 10px 5px; }
B. .result { width: 15px; }
C. Syntax error
D. .result { width: add(10px, 5px); }
Solution
Step 1: Understand the function behavior
The function add takes two values and returns their sum using @return $a + $b;.
Step 2: Apply the function in the style
Calling add(10px, 5px) returns 15px, so the CSS becomes .result { width: 15px; }.
Final Answer:
.result { width: 15px; } -> Option B
Quick Check:
Function returns sum, so width is 15px [OK]
Hint: Functions return values, so output replaces function call [OK]
Common Mistakes:
Expecting function name to appear in output
Confusing list output with addition
Thinking it causes syntax error
4. Identify the error in this Sass function:
@function multiply($x, $y) { return $x * $y; }
medium
A. Parameters must be strings
B. Function name is invalid
C. Missing @ before return keyword
D. Function cannot have two parameters
Solution
Step 1: Check the return statement syntax
In Sass functions, the return keyword must be @return, not just return.
Step 2: Verify other parts
The function name and parameters are valid, and multiple parameters are allowed.
Final Answer:
Missing @ before return keyword -> Option C
Quick Check:
Use @return, not return [OK]
Hint: Always use @return in Sass functions [OK]
Common Mistakes:
Using 'return' without @
Thinking parameters must be strings
Believing function names can't have multiple parameters
5. You want to create a Sass function shade-color that darkens a color by a percentage. Which of these function definitions correctly uses @function and @return to achieve this?