Functions are one of the best concepts in programming along with some other buddies like arrays, classes, objects, pointers and tone of more.
Functions are nothing but an abstraction of common code pattern.
Functions are a great way to make code reusable so quickly. You can define some complex operations in functions that can be reused throughout the stylesheet and you can import it to another modules too.
Functions are declared using the @function at-rule in SassScript.
SassScript Function Syntax
@function name(args) {
...
}
Any naming rules?
You can use any Sass identifier as name for your function.
Function names in SassScript treat underscores and hyphens as identical. This means that font-color and font_color both refer to the same function.
What’s inside a function?
A function can only contain universal statements. Universal statements are the statements that can be used anywhere in a Sass stylesheet.
Additionally, a function will have @return at-rule like any function that will return the result of the function call.
How to call the function?
You can use the CSS function syntax to call a function.
SCSS Function example
In this example, we are using a for loop inside a function to create a power function.
create examples functions reusability sass