Open In App
Related Articles

Sass @function Rule

Improve Article
Improve
Save Article
Save
Like Article
Like

Functions can define complex operations on SassScript values that can be reused throughout the stylesheet. It makes it easier to abstract out the common formulas and behaviors in some readable way. Functions can be defined using @function at-rule.

Syntax:




@function <function name>(<arguments...>) {
 ... 
}


A function name can be any Sass identifier. It must only contain universal statements, as well as the @return at-rule which indicates the value to use result of the function call. The normal CSS functions are used for calling functions.

Example:




@function power($base, $expo)
  $result: 1
  @for $_ from 1 through $expo
    $result: $result * $base
  @return $result
  
.sidebar
  float: right
  margin-left: power(6, 2) * 2px


Output:

.sidebar {
  float: right;
  margin-left: 64px;
}

Arguments: Arguments allow the function’s behavior to be changed every time that they’re called. The arguments are specified in the @function rule after the function name as shown in the syntax. It is simply a list of variable names surrounded by parentheses or curly brackets. The functions are always called with the same number of arguments which are in SassScript expressions. The values of these expressions are available within the function body as the corresponding variables.

Types of arguments:

  • Optional Arguments:
    Example:




    @function gfg($color, $amount: 100%) {
      $geeks: change-color($color, $hue: hue($color) + 180);
      @return mix($geeks, $color, $amount);
    }
      
    $primary-color: #ffffff;
    .header {
      background-color: gfg($primary-color, 80%);
    }

    
    

    Output:

    .header {
      background-color: white;
    }
    
  • Arbitrary Arguments:
    Example:




    $lengths: 50px, 30px, 100px;
    .gfg {
      width: max($lengths...);
    }

    
    

    Output:

    .gfg {
      width: 100px;
    }
    
  • Keyword Arguments:
    Example:




    $geeks: green;
    .banner {
      background-color: $geeks;
      color: scale-color($geeks, $lightness: +40%);
    }

    
    

    Output:

    .banner {
      background-color: green;
      color: #1aff1a;
    }
    

@return: The @return at-rule gives the value to be used as the result of the called function. It is only valid in the @function rule, and every @function must end with @return. When @return is called, it immediately ends the function and returns the result. Returning early are very useful for handling edge-cases. Its also useful in cases where an efficient algorithm is available without wrapping the entire function in an @else block.
Example:




@function add($numbers...) {
  $add: 0;
  @each $number in $numbers {
    $add: $add + $number;
  }
  @return $add;
}
  
.gfg {
  width: add(50, 30, 100);
}


Output:

.gfg {
  width: 180;
}

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 14 Jul, 2020
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials