1. Functions
Scala being a functional programming language is one of its core differentiating traits. Let's get familiar with functions before learning about the functional STYLE later.
2. Twenty-One
We'll continue writing useful code for our Twenty-One program.
3. Functions
In our experience teaching at DataCamp, we've learned that most learners require a good amount of practice to get proficient at defining functions.
In this INTRO course, you'll understand what functions are and use that understanding to call an already defined function.
4. What is a function?
So, what do functions do? Functions are invoked or "called" with a list of arguments, to produce a result. The parts of a function are the parameter list, the body, and the result type. All I want you to focus on in this course
5. What is a function?
is the body.
6. A specific question
Let's start with a question. Imagine we are playing Twenty-One, and we have two cards: a king and a ten. Does this hand bust? In the form of an expression, we write: twenty greater than twenty-one, which is a Boolean expression whose result is false, since twenty is NOT greater than twenty-one.
7. Generalizing that question
Now, for our Twenty-One program, our general "player" won't have a king and a ten every time. They will have two random cards,
8. Generalizing that question
sometimes three or more if they choose to hit, to get closer to twenty-one.
9. Generalizing that question
Instead of writing twenty greater than twenty-one, we write
10. Generalizing that question
hand greater than twenty-one, where hand represents that general hand's point value. hand greater than 21 is the body
11. The bust function
of a function called bust, which we'll use in our program. Here the function is defined in a script. See hand greater than twenty-one?
That's the function body. It follows the equals sign in a code block, where a code block is code surrounded by curly braces. Ignore the rest of the function for now. The colon Boolean part is optional in Scala,
12. The bust function
so I'm going to remove it to minimize distraction.
13. What do functions do again?
Reminder: functions are invoked, or called, to produce a result.
As mentioned previously, one of the defining features of Scala being functional is that functions are first-class values.
The equal sign that precedes the code block is a tell. All functions produce results, and all results have values, and all values have value types. In this case, the result is the Boolean value type: true or false,
which we can see by calling the function within the println() function. To call a function, you write the name of the function with the argument, or arguments plural if there are more than one, in parentheses.
Here, we get false for twenty, and true for twenty-two.
14. Call a function with variables
You can also pass in expressions as arguments.
Calling bust with the expression kingSpades plus tenHearts
results in the Boolean false. Functions being first-class values in Scala means you can even pass in OTHER FUNCTIONS as arguments to functions, which you'll see later.
15. Let's practice!
Practice time. Functions are core to the essence of Scala, so really focus during these exercises.