
- Scala - Home
- Scala - Overview
- Scala - Features
- Scala - Environment Setup
- Scala - Build Tool (SBT)
- Scala - REPL
- Scala - Dot & Dotty
- Scala - Basic Syntax
- Scala - Hello World Program
- Scala - Identifiers
- Scala - Keywords
- Scala - Comments
- Scala - Code Blocks
- Scala - Semicolon
- Scala - Constructs
- Scala - Expressions
- Scala - Input and Output
- Scala - Optional Braces
- Scala - Underscore (_)
- Data Types and Variables
- Scala - Data Types
- Scala - Type Bounds
- Scala - Context Bound
- Scala - Variances
- Scala - Type Hierarchy
- Scala - Variables
- Scala - Variable Scopes
- Scala - Literals
- Scala - Numeric Types
- Scala - Boolean Types
- Scala - Char Type
- Scala - Unit Types
- Scala - Strings
- Scala - Arrays
- Scala - Null Type
- Scala - Nothing
- Scala - Any Type
- Scala - AnyRef Type
- Scala - Unified Types
- Scala - Dates and Times
- Scala - Ranges
- Scala - Multidimensional Arrays
- Scala - WrappedArray
- Scala - StringBuilder
- Scala - String Interpolation
- Scala - StringContext
- Scala - Type Casting
- Scala var vs val
- Scala Operators
- Scala - Operators
- Scala - Rules for Operators
- Scala - Arithmetic Operators
- Scala - Relational Operators
- Scala - Logical Operators
- Scala - Bitwise Operators
- Scala - Assignment Operators
- Scala - Operators Precedence
- Scala - Symbolic Operators
- Scala - Range Operator
- Scala - String Concatenation Operator
- Scala Conditional Statements
- Scala - IF ELSE
- Scala - IF-ELSE-IF-ELSE Statement
- Scala - Nested IF-ELSE Statement
- Scala Loop Statements
- Scala - Loop Statements
- Scala - while Loop
- Scala - do-while Loop
- Scala - Nested Loops
- Scala - for Loop
- Scala - break Statement
- Scala - yield Keyword
- Scala Classes & Objects
- Scala - Classes & Objects
- Scala - Constructors
- Scala - Auxiliary Constructor
- Scala - Primary Constructor
- Scala - This Keyword
- Scala - Nested Classes
- Scala - Getters and Setters
- Scala - Object Private Fields
- Scala - Singleton Object
- Scala - Companion Objects
- Scala - Creating Executable Programs
- Scala - Stateful Object
- Scala - Enumerations
- Scala - Polymorphism
- Scala - Access Modifiers
- Scala - Extending a Class
- Scala Methods & Functions
- Scala - Functions
- Scala - Main Methods
- Scala - Functions Call-by-Name
- Scala - Functions with Named Arguments
- Scala - Function with Variable Arguments
- Scala - Recursion Functions
- Scala - Default Parameter Values
- Scala - Functions without Parameters
- Scala - Implicit Parameters
- Scala - Higher-Order Functions
- Scala - Nested Functions
- Scala - Extension Methods
- Scala - Anonymous Functions
- Partially Applied Functions
- Scala - Currying Functions
- Scala Collections
- Scala - Collections
- Scala - Lists
- Scala - Sets
- Scala - Maps
- Scala - Tuples
- Scala - Iterators
- Scala - Options
- Scala - Algebraic Data Types
- Scala Pattern Matching
- Scala - Pattern Matching
- Scala - Type Patterns
- Scala - Exception Handling
- Scala - Extractors
- Scala - Regular Expressions
- Scala Files I/O
- Scala - Files I/O
- Scala Advanced Concepts
- Scala - Closures
- Scala - Futures
- Scala - Promises
- Scala - Traits
- Scala - Trait Mixins
- Scala - Layered Traits
- Scala - Trait Linearization
- Scala - Sealed Traits
- Scala - Transparent Traits
- Scala - Literal Type Arithmetic
- Scala - Inline keyword
- Scala - Def, Var & Val
- Scala - Dropped Features
- Scala - BDD Testing
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 −
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 -
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 -
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 -
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 -
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.