Outdated Notice
In Scala, methods are defined inside classes (just like Java), but for testing purposes you can also create them in the REPL. This lesson will show some examples of methods so you can see what the syntax looks like.
Defining a method that takes one input parameter
This is how you define a method named double
that takes one integer input parameter named a
and returns the doubled value of that integer:
def double(a: Int) = a * 2
In that example the method name and signature are shown on the left side of the =
sign:
def double(a: Int) = a * 2
--------------
def
is the keyword you use to define a method, the method name is double
, and the input parameter a
has the type Int
, which is Scala’s integer data type.
The body of the function is shown on the right side, and in this example it simply doubles the value of the input parameter a
:
def double(a: Int) = a * 2
-----
After you paste that method into the REPL, you can call it (invoke it) by giving it an Int
value:
scala> double(2)
res0: Int = 4
scala> double(10)
res1: Int = 20
Showing the method’s return type
The previous example didn’t show the method’s return type, but you can show it:
def double(a: Int): Int = a * 2
-----
Writing a method like this explicitly declares the method’s return type. Some people prefer to explicitly declare method return types because it makes the code easier to maintain weeks, months, and years in the future.
If you paste that method into the REPL, you’ll see that it works just like the previous method.
Methods with multiple input parameters
To show something a little more complex, here’s a method that takes two input parameters:
def add(a: Int, b: Int) = a + b
Here’s the same method, with the method’s return type explicitly shown:
def add(a: Int, b: Int): Int = a + b
Here’s a method that takes three input parameters:
def add(a: Int, b: Int, c: Int): Int = a + b + c
Multiline methods
When a method is only one line long you can use the format shown, but when the method body gets longer, you put the multiple lines inside curly braces:
def addThenDouble(a: Int, b: Int): Int = {
val sum = a + b
val doubled = sum * 2
doubled
}
If you paste that code into the REPL, you’ll see that it works just like the previous examples:
scala> addThenDouble(1, 1)
res0: Int = 4
Contributors to this page:
Contents
- Introduction
- Prelude꞉ A Taste of Scala
- Preliminaries
- Scala Features
- Hello, World
- Hello, World - Version 2
- The Scala REPL
- Two Types of Variables
- The Type is Optional
- A Few Built-In Types
- Two Notes About Strings
- Command-Line I/O
- Control Structures
- The if/then/else Construct
- for Loops
- for Expressions
- match Expressions
- try/catch/finally Expressions
- Scala Classes
- Auxiliary Class Constructors
- Supplying Default Values for Constructor Parameters
- A First Look at Scala Methods
- Enumerations (and a Complete Pizza Class)
- Scala Traits and Abstract Classes
- Using Scala Traits as Interfaces
- Using Scala Traits Like Abstract Classes
- Abstract Classes
- Scala Collections
- The ArrayBuffer Class
- The List Class
- The Vector Class
- The Map Class
- The Set Class
- Anonymous Functions
- Common Sequence Methods
- Common Map Methods
- A Few Miscellaneous Items
- Tuples
- An OOP Example
- sbt and ScalaTest
- The most used scala build tool (sbt)
- Using ScalaTest with sbt
- Writing BDD Style Tests with ScalaTest and sbt
- Functional Programming
- Pure Functions
- Passing Functions Around
- No Null Values
- Companion Objects
- Case Classes
- Case Objects
- Functional Error Handling in Scala
- Concurrency
- Scala Futures
- Where To Go Next