Scala Tutorial - Learn How To Create Variable Argument Function - varargs _: *

By Nadim Bahadoor | Last updated: July 25, 2017 at 13:47 pm

Overview

In this tutorial, we will learn how to create functions which take variable arguments or varargs. In addition, you will learn how to pass-through Scala collection such as List or Sequence or even an Array to variable argument function using the syntax _: *.

Steps

1. How to define function which takes variable number of arguments

As a simple example, let's define a function named printReport() and it will have a single variable argument which represents the names of donuts for which we will print a report.


def printReport(names: String*) {
println(s"""Donut Report = ${names.mkString(", ")}""")
}

NOTE:

  • The names argument is of type String, but it is also a variable argument as we've defined it using the * syntax.
  • As a variable argument, you will be allowed to call the printReport() function by passing zero or many parameters of type String as shown in Step 2 below.

2. How to call function which takes variable number of String arguments

Since the printReport() function takes a variable argument called names of type String, you are allowed to call the function with zero or many String values.


println("\nStep 2: How to call function which takes variable number of String arguments")
printReport("Glazed Donut", "Strawberry Donut", "Vanilla Donut")
printReport("Chocolate Donut")

You should see the following output when you run your Scala application in IntelliJ:


Step 2: How to call function which takes variable number of String arguments
Donut Report = Glazed Donut, Strawberry Donut, Vanilla Donut
Donut Report = Chocolate Donut

 

3. How to pass a List to a function with variable number of arguments

What if you had a variable argument function and had to pass in a List to it?


println("\nStep 3: How to pass a List to a function with variable number of arguments")
val listDonuts: List[String] = List("Glazed Donut", "Strawberry Donut", "Vanilla Donut")
printReport(listDonuts)

NOTE:

  • Calling printReport() function by passing it the listDonuts value which is a List of type String will give you a compiler error!

Instead, you will need to use a special syntax called type ascription as follows:


printReport(listDonuts: _*)

You should see the following output when you run your Scala application in IntelliJ:


Step 3: How to pass a List to a function with variable number of arguments
Donut Report = Glazed Donut, Strawberry Donut, Vanilla Donut

 

4. How to pass a Sequence to a function with variable number of arguments

What if you had a variable argument function and had to pass in a Sequence to it?


println("\nStep 4: How to pass a Sequence to a function with variable number of arguments")
val seqDonuts: Seq[String] = Seq("Chocolate Donut", "Plain Donut")
printReport(listDonuts)

NOTE:

  • Calling printReport() function by passing it the seqDonuts value which is a Sequence of type String will give you a compiler error!

Instead, you will need to use a special syntax called type ascription as follows:


printReport(seqDonuts: _*)

You should see the following output when you run your Scala application in IntelliJ:


Step 4: How to pass a Sequence to a function with variable number of arguments
Donut Report = Glazed Donut, Strawberry Donut, Vanilla Donut

 

5. How to pass an Array to a function with variable number of arguments

What if you had a variable argument function and had to pass in a Array to it?


println("\nStep 5: How to pass an Array to a function with variable number of arguments")
val arrDonuts: Array[String] = Array("Coconut Donut")
printReport(arrDonuts)

NOTE:

  • Calling printReport() function by passing it the arrDonuts value which is a Array of type String will give you a compiler error!

Instead, you will need to use a special syntax called type ascription as follows:


printReport(arrDonuts: _*)

You should see the following output when you run your Scala application in IntelliJ:


Step 5: How to pass an Array to a function with variable number of arguments
Donut Report = Coconut Donut

This concludes our tutorial on Learn How To Create Variable Argument Function - varargs _: * and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to define function which takes variable number of arguments
  • How to call function which takes variable number of String arguments
  • How to pass a List to a function with variable number of arguments
  • How to pass a Sequence to a function with variable number of arguments
  • How to pass an Array to a function with variable number of arguments

Tip

Source Code

The source code is available on the allaboutscala GitHub repository.

 

What's Next

In the next tutorial, I will show you how to define functions whose names are symbols.

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Nephila Capital
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on