Scala – Procedures

  • date 26th April, 2021 |
  • by Prwatech |

Scala procedure and function differences


A Function, that doesn’t return anything can return a Unit that is comparable to void in Java and shows that function doesn’t return anything back. The functions which don’t return anything in Scala, they are procedures.

In Scala, procedures and functions are closely related concepts but have distinct characteristics based on their definitions and usage within the language.

A function in Scala is a name block of code that takes zero or more input parameters and produces a result when invoked. Functions are defined using the def keyword and explicitly specify a return type.  Functions in Scala always return a value, even if it’s Unit (similar to void in other languages).

On the other hand, a procedure in Scala is a special kind of function that does not return a value explicitly. Procedures are defined using the def keyword without specifying a return type (or specifying Unit explicitly). 

The distinction between functions and procedures highlights Scala’s functional programming capabilities and emphasizes the importance of clarity in side-effecting versus pure computations. While functions focus on producing results based on input parameters, procedures emphasize performing actions without necessarily returning a value. Understanding these differences is crucial for writing expressive and idiomatic Scala code.

use case 1:

scala> def rect_area(length:Float, breadth:Float) {val area = length*breadth; println(area)}

use case 2:

scala> def circle_area(radius:Float) {val area = 3.14*radius*radius; println(area)}

use case 3:



Quick Support

image image