Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, functions are first-class citizens. This means that they can be defined, passed as arguments, returned from other functions, and assigned to variables. Below, we'll explore the basics of defining and using functions in Scala.
The most basic way to define a function is using the def
keyword:
def greet(name: String): String = { "Hello, " + name + "!" }
Here, greet
is a function that takes a String
parameter called name
and returns a String
.
A function can have multiple parameters separated by commas:
def add(a: Int, b: Int): Int = { a + b }
You can also define a function with no parameters:
def sayHello(): String = { "Hello, world!" }
Scala supports type inference, so you can often omit the return type, and Scala will infer it for you:
def add(a: Int, b: Int) = a + b
However, in complex functions or when defining public APIs, it's a good practice to provide the return type explicitly for clarity.
Scala allows you to define functions without a name, often referred to as "anonymous functions" or "lambdas":
val multiply = (a: Int, b: Int) => a * b
Functions that accept other functions as parameters or return functions as results are called higher-order functions. This is a powerful concept in functional programming:
def applyFunction(a: Int, b: Int, f: (Int, Int) => Int): Int = f(a, b) val result = applyFunction(5, 3, (x, y) => x * y) // Result will be 15
You can define functions inside other functions:
def factorial(n: Int): Int = { def go(n: Int, acc: Int): Int = { if (n <= 0) acc else go(n - 1, n * acc) } go(n, 1) }
In this example, the go
function is a helper function used to compute the factorial in a tail-recursive manner.
Scala offers a robust system for defining and working with functions, marrying both object-oriented and functional programming principles. This provides developers with flexibility and expressiveness when constructing programs.
Introduction to Functions in Scala:
Functions in Scala are first-class citizens, meaning they can be assigned to variables, passed as arguments, and returned as values.
def add(x: Int, y: Int): Int = { x + y }
Parameters and Return Types in Scala Functions:
Specify parameters and return types in Scala functions.
def greet(name: String): String = { s"Hello, $name!" }
Anonymous Functions and Lambda Expressions in Scala:
Use anonymous functions or lambda expressions for concise function definitions.
val add: (Int, Int) => Int = (x, y) => x + y
Higher-Order Functions in Scala:
Functions that take other functions as parameters or return functions are higher-order functions.
def applyOperation(x: Int, y: Int, operation: (Int, Int) => Int): Int = { operation(x, y) }
Function Literals and Anonymous Functions in Scala:
Define function literals or anonymous functions without explicitly naming them.
val add: (Int, Int) => Int = _ + _
Function Application and Calling in Scala:
Call functions by providing arguments in parentheses.
val result = add(3, 5)
Recursion in Scala Functions:
Define recursive functions in Scala.
def factorial(n: Int): Int = { if (n <= 1) 1 else n * factorial(n - 1) }