Scala Foundation Course - Named Arguments and Default Values


We covered most of the complicated and confusing areas of Scala functions. In this session, I want to cover two simple but important techniques.

  1. Variable length argument.
  2. 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.

You can use it like this.

You can pass as many arguments as you want.

You must remember few important observations.

  1. The repeating argument must be the last argument. So, the below example is invalid.
  1. 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.
  2. 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.

It takes two parameters. A function and a string. I can use it as shown below.


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.

The named argument allows us to pass the input parameters in a different order. Let's try it.

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. However, It starts making sense as soon as you give a default value for an argument.
Let's start with the same example.

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.
That's it.
Thank you very much for watching Learning Journal. Keep Learning and Keep Growing.


You will also like: