← Back to Scala Course | Chapter 5: Functions | Lesson 3 of 7

Default Parameters

In this page:

  1. Default Parameters

Default Parameters

A parameter can specify a default value in its declaration, def greet(name: String, greeting: String = "Hello"), making it optional at the call site. If the caller doesn't supply it, the default is used. Default parameters are commonly combined with named arguments to skip earlier defaults while still setting a later one.

Note: Default parameters pair naturally with named arguments -- you can override just one default deep in the parameter list without repeating the others.

Example: Default Parameters

markup
object Main extends App {
  def greet(name: String, greeting: String = "Hello"): Unit = {
    println(s"$greeting, $name!")
  }

  greet("Alice")
  greet("Bob", "Welcome")
}
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.