×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Parameterless method in Scala

By IncludeHelp Last updated : October 20, 2024

Scala parameterless method

A method which accepts no parameters from the calling code. It also denotes that there will not be any empty parentheses. These are special types of methods in Scala that are initialized and called especially. To initialize parameterless method you need to use the name with def keyword but w/o the "( )" parenthesis. While calling the method also we will avoid using the "( )" parenthesis.

Syntax

Follow this syntax to define a parameterless method:

def method_name = code_to_be_executed

Example of Scala Parameterless Method

class calculator(a: Int, b: Int) 
{ 
    def add = println("a+b = " + (a+b)) 
    def subtract = println("a-b = " + (a-b)) 
    def product = println("a*b = "+ (a*b) )
    def division = println("a/b = " + (a/b))
} 
object Main 
{ 
    def main(args: Array[String]) 
    { 
        val calc = new calculator(1250, 50) 
        println("value of a = 1250 and b =  50")
        calc.add
        calc.subtract
        calc.product
        calc.division
    } 
} 

Output

value of a = 1250 and b =  50
a+b = 1300
a-b = 1200
a*b = 62500
a/b = 25

Explanation

This code initializes a class calculator and defines 4 parameterless methods in it. These methods are used to add, subtract, multiply and divide values. These methods directly print the calculated values and do not accept any parameters. The object calc of the class calculator is created with values 1250 and 50. Then with this calc object, we will call all the four methods that perform operations and returns output.

Calling Parameterless Method

The parameterless method is invoked without the () parenthesis. And there is an error if we call a parameterless method using the parenthesis.

Calling Parameterless Method Using Parenthesis

If the programmer by mistake puts a parenthesis at the end of the function call, the compiler would report an error stating: "Unit does not take parameters".

Example

Program to show error when parameterless method is called using parentheses:

class calculator(a: Int, b: Int) 
{ 
    def division = println("a/b = " + (a/b))
} 
object Main 
{ 
    def main(args: Array[String]) 
    { 
        val calc = new calculator(1250, 50) 
        println("value of a = 1250 and b =  50")
        calc.division()
    } 
} 

Output

/home/jdoodle.scala:12: error: Unit does not take parameters
		calc.division()
                             ^
one error found

Explanation

This code has the same logic as the former code. The difference is just that while calling the division parameterless method parenthesis is used. Due to this, the compiler gives an error: "Unit does not take parameters".

Parameterless Method Vs. Method Without Parameter

Scala has supports multiple types of parameter passing in its methods.

But these two methods look quite similar but have different functionalities. The parameterless method does not accept and sort of parameter in its call. But the method without parameter accepts void parameter in its call ( nothing can be treated as a parameter).

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.