Monday, October 13, 2014

Scala - Functions

As we know functions are group of statements to perform a piece of operation/task. We will quickly dive into the details instead of long definitions.

Syntax

def is the keyword used to define the function.
def function_name ([param1:type]{,param2:type}):returnType = {
    [Statements]
    return value;
}

Example

def sum(x:Int, y:Int) : Int =
{
   return x + y;
}

// Calling the function
val x:Int = 20;
val y:Int = 30;
var z:Int = sum(x,y);

println("Sum is "+z);
This exactly looks the way we define and call in other languages but Scala simplified the definition by making some of the tokens optional.
  • Type of the return value is optional. So function prototype can be without :returnType after arguments
  • return keyword is optional.
  • If there is only one line in the function, then braces are options. Braces required to say it's a block
Now, the above function can be defined as
def sum(x:Int, y:Int) = x + y;

Anonymous Functions

Scala provides a very simple and convenient way to define the anonymous functions. See below for an example
val sum = (x:Int, y:Int) => x + y; // val to be used instead of def keyword.

//Calling
println(sum(15,50)); //Prints 65

  • In case of anonymous functions, we don't need def keyword. We have to use val keyword instead.
  • Calling the function is same as the normal functions.
One more point here is, the parameter names are optional in case of anonymous functions. So, we can define the function as
val add5:(Int,Int)=> Int = _ + _; // No argument names

//Call the function
println(sum(15,50)); //calling is same.

Return Tuples 

Scala allows us to return multiple values as a tuple. Let's see an example of how to swap two strings using a tuple.
def swap(x:String, y:String):(String, String) = { return (y, x)}; //Returning in reverse order 
def swap2(x:String, y:String) = (y, x); //Removed optional tokens

//Calling
val (a,b) = swap("Hi","Scala");
println(a,b); //Prints Scala, Hi

val (c,d) = swap2("Hi","Scala");
println(c,d); //Prints Scala, Hi

Variable arguments 

Variable arguments can be specified to function using the operator *. Example shows the syntax and usage.
def printArgs(x:String*) = {
  for(a <- x)
  {
     println(a);
  }
}

//Calling
printArgs("Hi", "Hello", "Scala"); //Prints the strings in order.

Default and Named arguments 

Scala allows to default some of the arguments and also to pass the arguments based on the name of the parameters instead of the order
def increment(x:Int, y:Int = 1) = x + y; // Defaults the argument y to 1 if not passed

//Calling
println(increment(20)); //Increases by 1 because not passed
println(increment(20,5)); //Increases by 5 because 5 is passed

def printValues(x:String, y:Int) = {
   println(" X = "+x+" : Y = "+y);
}

//Calling
printValues(x="Hi", y=20); // Prints X = Hi : Y = 20
printValues(y=20, x="Hi"); // Prints X = Hi : Y = 20 - Even order is different because of named arguments
That's it for the current post. Happy Learning!!!

No comments:

Post a Comment