Scala - Functions Without Parameters (Parameterless Method)



Functions without parameters are used if function behavior is constant and does not depend on any external input to produce a result. Functions without parameters are also known as parameterless methods. So, there is clarity when you define operations that do not require any input.

Functions without Parameters

You can define functions without any parameters. So, these functions can also be invoked without passing any arguments. These are used when the function logic is independent of any input values.

Definition

Functions without parameters do not take any arguments. You can ignore the parameter list in the function definition. These functions can be called without any arguments.

Syntax

The syntax of the function that do not have any parameters -

def functionName(): ReturnType = { // function body }

Example: Property-like Functions

The following example shows defining and using a function without parameters in Scala −

Open Compiler
object Demo { def greet: String = { "Hello, World!" } def main(args: Array[String]): Unit = { println(greet) // Hello, World! } }

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

> scalac Demo.scala > scala Demo

Output

Hello, World!

In the example, the greet function does not take any parameters. It returns simple greeting message. The main method calls greet and prints the result.

Functions without Parentheses

You can also define a function without parentheses if they do not take any parameters. So these functions can be called without parentheses as well. These methods are used to represent properties or characteristics of an object.

Syntax

The syntax of a function without parentheses -

def functionName: ReturnType = { // function body }

Example: Property-like Functions

The following example shows defining and using a function without parentheses in Scala -

Open Compiler
object Demo { def currentTime: Long = { System.currentTimeMillis() } def main(args: Array[String]): Unit = { println(currentTime) // Prints the current time in milliseconds } }

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

> scalac Demo.scala > scala Demo

Output

<current time in milliseconds>

In the example, the currentTime function is defined without parentheses because there is no parameter required in this method. It returns the current time in milliseconds. The main method calls currentTime and prints the result.

Benefits of Functions without Parameters

You can have code readability and simplicity by removing unnecessary arguments which is possible in functions without parameters. You can represent ideally constant values, properties, and operations that do not require input.

Example: Constant Values

The following example shows using functions without parameters to represent constant values -

Open Compiler
object Demo { def pi: Double = 3.14159 def main(args: Array[String]): Unit = { println(s"The value of pi is $pi") // The value of pi is 3.14159 } }

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

> scalac Demo.scala > scala Demo

Output

The value of pi is 3.14159

In the example, the pi function represents the mathematical constant π. It does not take any parameters. The main method prints the value of pi.

Parameterless Functions vs. Methods with Empty Parentheses

You can define parameterless functions either without parentheses or with empty parentheses. It depends on the nature of the function. Parameterless functions without parentheses are used for properties. Whereas functions with empty parentheses are used for actions and computations that do not require input but may have side effects.

Syntax

Without parentheses -

def functionName: ReturnType = { // function body }

With empty parentheses -

def functionName(): ReturnType = { // function body }

Example: Property-like vs. Action-like Functions

The following example shows both approaches in Scala programming -

Open Compiler
object Demo { def constantValue: Int = 42 // Property-like function def printMessage(): Unit = { // Action-like function println("This is a message") } def main(args: Array[String]): Unit = { println(s"The constant value is $constantValue") // The constant value is 42 printMessage() // This is a message } }

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

> scalac Demo.scala > scala Demo

Output

The constant value is 42
This is a message

In the example, constantValue is defined without parentheses as it represents a constant. Whereas printMessage is defined with empty parentheses as it represents an action.

When to Use Parameterless Functions

You can use parameterless functions to define properties and constants that do not change and do not require input.

Example: Constant Definition

Consider following example of parameterless function in Scala programming -

object Demo { def appName: String = "Tutorialspoint" def main(args: Array[String]): Unit = { println(s"Website Name: $appName") // Website Name: Tutorialspoint } }

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

> scalac Demo.scala > scala Demo

Output

Website Name: Tutorialspoint

You can also use functions with empty parentheses to define actions and computations that may have side effects and require explicit invocation.

Example: Computation Definition

Consider following example of parameterless function in Scala programming -

Open Compiler
object Demo { def computeSum(): Int = { 5 + 10 } def main(args: Array[String]): Unit = { println(s"The sum is ${computeSum()}") // The sum is 15 } }

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

> scalac Demo.scala > scala Demo

Output

The sum is 15

Parameterless Method Summary

  • You can define functions without parameters if the function does not require any parameter explicitly.
  • These functions are invoked without any arguments.
  • Functions without parameters can be two types: Parameterless functions and Methods with parentheses.
  • The choice between parameterless functions and methods with empty parentheses depends on whether the function represents a property or an action.
  • You can use functions without parameters for actions that do not require any input. Examples are: logging, getting system properties, performing fixed calculations, etc.
Advertisements