Named Arguments and Default Parameter Values in Scala

Topic Overview

Welcome! This Scala lesson demystifies two key features: named arguments and default parameter values. They make function calls clearer and the code more maintainable.

Named Arguments in Scala

Default Parameter Values in Scala

Default parameter values automatically assign a predefined value to a function parameter. They permit the skipping of parameters that aren't required for the function call.

Consider a function that greets a user:

Scala
def greetUser(name: String = "Guest"): Unit =
    println(s"Hello, $name")

@main def run: Unit =
    greetUser()                  // Displays "Hello, Guest"
    greetUser("John")            // Displays "Hello, John"
    greetUser(name = "Emily")    // Displays "Hello, Emily"

The first function call uses the default value, as no argument is passed.

Combining Named Arguments and Default Parameter Values

Lesson Summary and Upcoming Practice

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal