Prerequisites – Scala | Functions
A parameterless method is a function that does not take parameters, defined by the absence of any empty parenthesis. Invocation of a paramaterless function should be done without parenthesis. This enables the change of def to val without any change in the client code which is a part of uniform access principle.
Example:
// Scala program to illustrate // Parameterless method invocation class GeeksforGeeks(name: String, ar: Int) { // A parameterless method def author = println(name) def article = println(ar) // An empty-parenthesis method def printInformation() = { println("User -> " + name + ", Articles -> " + ar) } } // Creating object object Main { // Main method def main(args: Array[String]) { // Creating object of Class 'Geeksforgeeks' val GFG = new GeeksforGeeks("John", 50) GFG.author // calling method without parenthesis } } |
Output:
John
There are generally two conventions for using parameter less method. One is when there are not any parameters. Second one is when method does not change the mutable state. One must avoid the invocations of parameterless methods which look like field selections by defining methods that have side-effects with parenthesis.
An example of calling a parameterless method with a parenthesis giving a Compilation error.
Example:
// Scala program to illustrate // Parameterless method invocation class GeeksforGeeks(name: String, ar: Int) { // A parameterless method def author = println(name) def article = println(ar) // An empty-parenthesis method def printInformation() = { println("User -> " + name + ", Articles -> " + ar) } } // Creating object object Main { // Main method def main(args: Array[String]) { // Creating object of Class 'Geeksforgeeks' val GFG = new GeeksforGeeks("John", 50) GFG.author() //calling method without parenthesis } } |
Output:
prog.scala:23: error: Unit does not take parameters
GFG.author() //calling method without parenthesis
^
one error found
Note: An empty parenthesis method can be called without parenthesis but it is always recommended and accepted as convention to call empty-paren methods with parenthesis.
Recommended Posts:
- Scala String indexOf(String str) method with example
- Scala String contentEquals() method with example
- Scala Int /(x: Int) method with example
- Scala Int /(x: Short) method with example
- Scala Map size() method with example
- Scala SortedMap addString() method with a start, a separator and an end with example
- Scala Iterator addString() method with example
- Scala String substring(int beginIndex, int endIndex) method with example
- Scala Set &() method with example
- Scala Int <(x: Char) method with example
- Scala Int <=(x: Double) method with example
- Scala Int <=(x: Byte) method with example
- Scala Set dropRight() method with example
- Scala Set equals() method with example
- Scala Set drop() method with example
- Scala Set dropWhile() method with example
- Scala Set diff() method with example
- Scala Set copyToArray() method with example
- Scala Int unary_+ method with example
- Scala Int unary_- method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

