Scala Unit

Just like many other object-oriented programming languages, Scala often uses return types that are very popular among programmers and are used almost in every program to make it more secure and authentic. Return types are known for returning the result to the calling function of the statement in each program. The Scala has a “Unit” return type that doesn’t return anything to the calling statement. You can say that the “Unit” type is equivalent to the “void” in Java programming. So, we will make use of the “Unit” type in Scala programming in this tutorial to elaborate on its use. First, to update the Linux system, we try the “update” instruction in its terminal application that is launched by a quick shortcut “Ctrl+Alt+T”.

Install Java-JRE

Make sure to install the Java environment on your system before the installations and implementations of Scala. Without Java, we will be unable to use Scala on our end. Therefore, we cast off the apt package within the Linux “install” query with the keyword “default-JRE” to install all the required packages of the Java environment. Add the password of your logged-in user if asked.

Tap “y” upon rthe request “Do you want to continue?” if you want to continue the installation.

It might be more time-consuming than the typical installations to complete.

After the complete installation, you are ready to move further.

Install Scala Package

It’s time to install the packages of Scala to enable it on your Linux system. For this, we make use of the simple install instructions with the “apt” module and keyword “scala” as shown. It asks for the root password and you have to pass in the password to continue.

Tap “y” once again upon the confirmation question is asked.

The Scala is successfully configured in our Ubuntu 20.04 system and is ready for use.

You can simply use the Scala command line or create a Scala file to add a code to it to continue.

Example 1:

We start our first example of Scala to use the “Unit” type in its program via the Scala file. We cerate a new Scala file named “Hello.scala” via the “touch” query used in the terminal. The “ls” list instruction shows the newly created file in the current working directory of our Linux system. To open the “Hello.scala” file, we choose the manual way of opening the files, i.e. through the file explorer via the text editor.

As we mentioned in the introduction paragraph that the Unit type is the same as “void” or no return type, we use it within our first example to elaborate on its use in Scala programming. We start our program with the use of a random object named “Hello” that encompasses the “Apps” class from the “Scala” using the keyword “extends”.

This “Hello” object utilizes a user-defined function and some code lines. We defined the function “Get” using the keyword “def” and added a single parameter “m” of string type to it, i.e. you can pass a string value to it. The “Unit” type is used with it which implies that this function doesn’t return any value. Within this user-defined function, we use the println() function to print out the value “m” passed by the function call from the outside.

After this user-defined function, we tried a print statement to let us know that it is going to display the output. The next println() statement calls the “Get” function within it by passing it a string value “Raza”. Let’s save the following Scala code:

After saving the Scala code, we compile the Scala file “Hello.scala” using the “scalac” compiler in the terminal. The object file of the compiled code is created with a name that is the same as the class “Hello” mentioned in the Scala file. Use the “scala” keyword along with the “Hello” object file to execute the compiled code. The output shows the execution of the Get function taking argument from the outside but doesn’t return any value.

Example 2:

Let’s take a look at the example to return some value to the calling function more clearly using the Scala code. We start this example code with the use of the object “Hello” which uses two functions within it. The first function “Get” is the user-defined function that takes three integer values from the main() function – m, n, and r. The second function is the main() function that is used to start the overall Scala program execution. The main() function uses the println() function line to tell us that it calls the “Get” function by passing it some values.

In the 8th line, we use the variable “z” to get the returned result from the Get() function call by passing it the three integer values. The Get() function displays that we are inside the “get” function and calculates the multiplication result of the three values saved to the “res” variable. The println() statement at the 10th line is used to display the returned result. Let’s save the code to get what occurs:

Upon compilation and execution of the previous Scala code, we got the following presented output. It first executed the main() function and passed the three values to the “Get” function. It calculated the multiplication result by using three values but didn’t return it to the main() function as per the use of the “Unit” type. In return, the println() statement shows the empty brackets ().


The introduction of this article elaborates on the use of the return types in programming, especially the similarity between the “unit” type of Scala and the “void” type of Java. Therefore, we installed and configured the Java-JRE and Scala Packages on our Linux system and performed some useful examples. We used the “unit” type in both of our mentioned examples to discuss how it can be used so far to return nothing to its calling function or statement.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.