Home » Scala language

Parameterless method in Scala

Scala parameterless method: Scala provides using a special type of method that is a parameterless method which does not accept any parameters. In this tutorial, we will learn to use a parameterless method with working code.
Submitted by Shivang Yadav, on August 17, 2019

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:

    def method_name = code_to_be_executed

Program to illustrate 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.

The parameterless method is invoked without the () parenthesis. And there is an error if we call a parameterless method using the 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".

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".

Bonus : 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.