Functions as First-Class Values
Learn to define and pass functions as arguments, return them from other functions, and store them in variables.
Functions as First-Class Values
Welcome to Functional Programming Core! In Scala, functions are not just commands; they're first-class values.
This means you can treat functions just like any other data type, such as numbers or strings. This is a core concept in functional programming!
Assigning Functions to Variables
Just like you can assign a number to a val, you can assign a function to one. Here, greet becomes a variable holding our function.
Try running this simple example:
object Main {
def main(args: Array[String]): Unit = {
val greet = (name: String) => s"Hello, $name!"
println(greet("Alice"))
}
}All lessons in this course
- Functions as First-Class Values
- Higher-Order Functions & Currying
- Immutability and Side Effects