Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

williamrossi
williamrossi
6,232 Points

Sass Functions

Hey guys, have no idea with this one, the video didn't really help me!

Use the double function you just wrote to refactor this code: div { font-size: 5px * 2; }

Any breaking down would be helpful , or full answer, either one is good 80]

4 Answers

Hi, William Lyth:

When it comes to functions in Sass, like most languages , you have functions that accepts arguments. Arguments are alternatively called parameters.

Functions are referred by what they were called during the definition of them. In Sass, you explicitly define a function like this in .scss syntax :

// SCSS Syntax 
@function name_of_new_function($parameter_1) {
   // do what you wanted to do within this function in the first place 
   @return $output_of_this_function_you_want 
}

and in .sass syntax

// Sass syntax 
@function name_of_new_function($parameter_1) 
  // do what you wanted to do within this function in the first place 
   @return $output_of_this_function_you_want 

To have the function work (be executed or be invoked), you use a set of parentheses alongside the name of function. In this case, name_of_new_function(). However, this would be an error--we said name_of_new_function expects a parameter (in Sass you can define a default value, but for the sake of being simple, I'll ignoring taking that into consideration with this example).

So in this case, we need to pass in a parameter like this name_of_new_function(12). Depending on what your functions does inside of itself you should get a predictable output of a specific value and type with no problem.

For this problem, you have to execute a function called double. Like a function that was designed well, it's name indicated pretty well what it can do.

Like my example, double expects a parameter, which will be 5px in this case. This would replace what's currently there, 5px *2 (double(5px)).

Did all of that make sense to you, or was it in tl;dr; territory?

Hi William,

I'm not sure from your post if you aren't sure what your function does and how do you use it or if you're just confused by what it means to refactor the code. Since Kevin Lozandier gave a detailed answer on how to write and use functions I'll answer based on the assumption that you weren't sure what it meant to refactor the code.

I think what they want you to realize is that the font-size is going to work out to be 10px. So instead of arriving at 10px by doing 5px* 2 they want you to figure out a way to arrive at 10px by using your double function written in the previous task. You need to pass in the correct value as an argument so that the double function will return a value of 10px.

In general, when refactoring code, you're rewriting it in such a way that it still produces the same results.

williamrossi
williamrossi
6,232 Points

Thanks for the reply guys, made a lot more sense, I need to stop going on treehouse at 11pm. I'll give this one another go in a sec.

Robbie Thomas
Robbie Thomas
31,093 Points

You can also try this:

div { font-size: double(5px); }