We covered most of the complicated and confusing areas of Scala functions. In this session, I want to cover two simple but important techniques.
- Variable length argument.
- Default values and Named arguments
Let's start with the first one.
Variable length arguments in Scala
Most of the programming languages support the notion of variable length argument. Scala is not an exception. It allows you to indicate that the last argument of a function is a variable length argument. So, it may be repeated multiple times. Here is an example.
def echo(s:String*) = s foreach println
You can use it like this.
You can pass as many arguments as you want.
echo("One","Two","Three", "Four", "Five")
You must remember few important observations.
- The repeating argument must be the last argument. So, the below example is invalid.
def echo(s:String*, i:Int) = s foreach println
- The next observation is obvious. All these values are of the same data type. If the variable argument is of type Int, you can pass n number of Integers. But you can't mix in a string with them.
- Inside the body, s is an array. When the type of s is a String, inside the body, s is an array of string. When the type of s is an Int, inside the body, s would be an array of Int.
That's it. Let's move to the next item.
Named arguments in Scala
Let me start with the named arguments. I will show you an example and explain Scala syntax for the same. So, here is my example.
def doSomething(f: String => Unit, s:String) = f(s)
It takes two parameters. A function and a string. I can use it as shown below.
doSomething(x =>println("[" + x + "]") , "Hi There!")
The first parameter is always a function value and the second one is a string. By default, you will always pass the parameters in the order specified at the time of function definition. If I try changing the order, I will get an error.
doSomething("Hi There!", x =>println("[" + x + "]") )
The named argument allows us to pass the input parameters in a different order. Let's try it.
doSomething(s="Hi There!", f = x =>println("[" + x + "]"))
The name of the string parameter is s. I gave this name at the time of function definition. What is the name of the function parameter? The f. Right? The above example works. I can call the function even after changing the order of parameters.
Default values in Scala
The named argument doesn't make much sense without a default value. I mean, why would I want to
change the order of the parameter?
There must be some reason. Right? It doesn't make any sense doing something without reason.
It starts making sense as soon as you give a default value for an argument.
Let's start with the same example.
def doSomething(f: String => Unit=println, s:String) = f(s)
You can assign a default value for a parameter using an equal to symbol. I gave a default value
for the first parameter.
If the caller doesn't pass any value for the first parameter, Scala will take the default value as println. That's it.
Now, if I want to skip the first parameter and supply only the second parameter, I get an error. Not enough arguments. Try it and check yourself. There comes the named argument.
Use a named argument, and you can skip the first parameter because It has a default value. The
moral of the story is this.
To make some parameters optional, use the default value to define them and then use named arguments to call them. Simple as that.
Thank you very much for watching Learning Journal. Keep Learning and Keep Growing.