Scala method explained in detail

 199 total views,  2 views today

Welcome to Scala series. This tutorial is about the Scala method explained in detail.

A method in Scala is a sequence of statements wrapped to form a method.

Each method performs a certain task according to its sequence of statements.

In Scala, a method can be either written in a class or in an Object.

Scala method can have default values for the arguments.

Scala method is similar to Scala functions but with a few differences. A Scala method always starts with def keyword, it will have a method name, signature, arguments and return type. Although, in many cases, method and function are used interchangeably. Both do the same job but with a few differences.

Scala method is similar to the Java method but with a few changes.

Syntax

def methodname(arguments):returnType = {
 //statements
}

Description

  1. Every Scala method starts with the def keyword. def keyword defines the method.
  2. Method name should be in camelCase.Although it is not mandatory to be in camelCase but worldwide it is a standard to write a method name in camelCase.
  3. A method can have multiple arguments of different types.
  4. A method can have a return type.
  5. If the method doesn’t return anything, Unit can be mentioned but it is optional.

Example

def run(i:Int):Int = {
  println("running "+i)
}

In the above example, the method doesn’t return anything. We can simply avoid marking the method as Unit when it is not having any return value.

A Method with a single line of expression

If the method has only one line of the statement, then we can avoid the curly braces and written as below.

def add(i: Int, j: Int, z: Int): Int = i + j + z

A Method with return type

This is a simple method which accepts two arguments as Integer, sums the arguments and returns the result as an Integer.

def add(i: Int, j: Int): Int = {
    i + j
}

A Method with no-return type

This is a simple method which accepts two arguments as Integer, sums the arguments and but returns nothing.

 def add(i: Int, j: Int): Unit = {
    i + j
 }

The above example can be simplified as below.

def add(i: Int, j: Int) = {
    i + j
}

No need for mentioning ‘:Unit’.

What is the best approach of no-return type?

It is better to use the Unit as it is more readable for the method doesn’t return any value.

A method with the default value for the arguments

object StaticReference {

  def main(ar1: Array[String]): Unit = {
    println(add(z = 2))
  }

  def add(i: Int = 5, j: Int = 8, z: Int): Int = {
    i + j + z
  }
}

Output

15

A method calling another method

object StaticReference {

  def main(ar1: Array[String]): Unit = {
    println(add(1, 2, 3))
  }

  def add(i: Int, j: Int, z: Int): Int = {
    i + j + z
  }
}

Output

6

Specify Parameter names while calling the method

object StaticReference {

  def main(ar1: Array[String]): Unit = {
    println(add(i = 1, j = 2, z = 3))
  }

  def add(i: Int, j: Int, z: Int): Int = i + j + z
}

Output

6

References: Official documentation

Thanks for reading and hope you like this tutorial. Please leave a comment and give us a thumbs up!

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.