Scala - Functions with Named Arguments


Advertisements

In a normal function call, the arguments in the call are matched one by one in the order of the parameters of the called function. Named arguments allow you to pass arguments to a function in a different order. The syntax is simply that each argument is preceded by a parameter name and an equals sign.

Try the following program, it is a simple example to show the functions with named arguments.

Example

object Demo {
   def main(args: Array[String]) {
      printInt(b = 5, a = 7);
   }
   
   def printInt( a:Int, b:Int ) = {
      println("Value of a : " + a );
      println("Value of b : " + b );
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

Value of a :  7
Value of b :  5
scala_functions.htm

Useful Video Courses


Video

Scala Online Training

82 Lectures 7 hours

Arnab Chakraborty

Video

Apache Spark with Scala - Hands On with Big Data

23 Lectures 1.5 hours

Mukund Kumar Mishra

Video

Delta Lake with Apache Spark using Scala

52 Lectures 1.5 hours

Bigdata Engineer

Video

Apache Spark with Scala useful for Databricks Certification

76 Lectures 5.5 hours

Bigdata Engineer

Video

Machine Learning with Apache Spark 3.0 using Scala

69 Lectures 7.5 hours

Bigdata Engineer

Advertisements