Scala functions - named arguments and default arguments

A nice feature of Scala is that in addition to letting you call and define functions just like you do in Java, you can also use both named arguments and default arguments. In this brief tutorial we'll look at both of these language features.

With Scala's named arguments (parameters), you can specify the names of function or method arguments when calling the method. Here's an example:

object NamedArguments {

  def main(args: Array[String]) {
    printName(firstName="Alvin", lastName="Alexander")
  }
  
  def printName(firstName:String, lastName:String) {
    System.out.format("Your name is %s %s\n", firstName, lastName)
  }
  
}

As you can see from this example, I create the printName function in the usual fashion, and then I can call it like this:

printName(firstName="Alvin", lastName="Alexander")

or with this formatting if you prefer:

printName(firstName="Alvin", 
          lastName="Alexander")

Here I use the function parameter names when calling the function. If you've used languages like Objective-C, you've seen this syntax before. Although it's more verbose, it can also make your code easier to read.

However, if you don't like the named parameter approach, a very nice thing about Scala is that this syntax is completely optional, and you can still call your function without named parameters:

printName("Alvin", "Alexander")

Default function argument values

When writing a Scala function, you can also declare default values for your function arguments. To do this, all you have to do is supply the argument's default value after declaring the argument, like this:

def printName(firstName:String = "Unknown", lastName:String = "Unknown") {
  System.out.format("Your name is %s %s\n", firstName, lastName)
}

After you've declared your function or method like that, you can then call your function in a variety of ways, as shown here:

printName("Alvin", "Alexander")
printName("Alvin")
printName()

These three calls result in the following output:

Your name is Alvin Alexander
Your name is Alvin Unknown
Your name is Unknown Unknown

(Admittedly this isn't a great example, but it's all I could think of at the moment.)

Putting those examples together, here's a small but complete Scala example that demonstrates both named function parameters and default function parameters:

object DefaultFunctionArguments {

  def main(args: Array[String]) {
    // a basic method call
    printName("Alvin", "Alexander")

    // using named function parameters
    printName(firstName="Alvin", lastName="Alexander")
    printName(lastName="Alexander", firstName="Alvin")  // you can change the order

    // relying on default function arguments
    printName("Alvin")
    printName()
  }
  
  def printName(firstName:String = "Unknown", lastName:String = "Unknown") {
    System.out.format("Your name is %s %s\n", firstName, lastName)
  }
  
}

Scala named arguments and default argument values

I hope this brief tutorial on Scala named parameters and default parameter values has been helpful. As mentioned, you don't have to use any of this syntax, but if you come from a variety of other programming languages, like Objective C, these Scala language features can make you feel more at home.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.