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

Named Arguments

In this page:

  1. Named Arguments

Named Arguments

Calling a function with paramName = value lets you pass arguments in any order and makes the call site self-documenting, especially useful for functions with many parameters of the same type. Named and positional arguments can be mixed, as long as positional ones come first. This is particularly handy combined with default parameters, letting you skip over earlier defaults.

Note: Named arguments make a call like connect(host = "localhost", port = 8080) far clearer than two unlabeled positional values.

Example: Named Arguments

markup
object Main extends App {
  def describe(name: String, age: Int, city: String): Unit = {
    println(s"$name is $age years old and lives in $city")
  }

  describe(name = "Priya", city = "Mumbai", age = 28)
}
🔒

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.