Functional programming is a programming style in which a user can write application programs by using functions and immutable value. The structure of Functional programming is declarative and like a tree that returns a value. It is based on mathematical functions.
Scala is a functional programming language in which a function is an ordered and reusable set of statements that are used to perform a certain action. Scala provides support for function as well methods in which a method is a part of a class that has its name and signature on the other side function is represented as an object that can be assigned to a variable.
How to Declare a Function?
The following is an example of a Function declaration that will have a function name, list of statements, and return type.
def nameoffunction ([parameters]) : [return type] = {
function body
return [expression]
}
How to Call a Function?
We can call a function with its name and parameter.
Nameoffunction(parameters)
Let us understand the function with the below example. Save the program with functionexample.scala name and run it from the terminal by using scala functionexample.scala command(refer screenshot).
object functionexample {
def main(args: Array[String]) {
println( "Returned Value : " + subtrint(10,5) );
}
def subtrint( x:Int, y:Int ) : Int = {
var subt:Int = 0
subt = x - y
return subt
}
}
Output
Click Here To Download Code File.
Scala Function Evaluation
The following are a different kind of evaluation strategy provided by Scala.
1. Scala Function Call-by-Name
Using the Call By Value Scala Evaluation Function an argument is submitted to the function body and it is evaluated based on its appearance in the function.
Let us understand the call-by-name function with the below example. Save the program with callbyvaule.scala name and run it from the terminal by using scala callbyvaule.scala command(refer screenshot).
object callbyvaule
{
def main(args: Array[String])
{
def ArticleCounts(i: => Int)
{
println("Count of number = " + i)
println("Count of number = " + i)
println("Count of number = " + i)
println("Count of number = " + i)
println("Count of number = " + i)
println("Count of number = " + i)
println("Count of number = " + i)
println("Count of number = " + i)
println("Count of number = " + i)
println("Count of number = " + i)
}
var result = 0;
ArticleCounts {
result += 1 ; result
}
}
}
Output
Click Here To Download Code File.
2. Scala Recursion function
Scala recursion is a method that is used to solve a problem in which the solution is dependent on the solutions to smaller instances of the same problem and it is achieved by allowing a function to call itself from its code.
Let us understand the recursion function with the below example of factorial. Save the program with recursionExample.scala name and run it from the terminal by using scala recursionExample.scala command(refer screenshot).
object recursionExample
{
def fact(y:Int): Int = {
if(y == 1) 1
else y * fact(y - 1)
}
def main(args:Array[String]) {
println(fact(3))
}
}
Output
Click Here To Download Code File.
3. Scala Function Default Parameter Values
In Scala, We can provide a default value for function parameters so, during the function call, the arguments for such parameters are omitted and filled with the result. If we want to specify a parameter then the first argument is taken from the parameter and the second value is taken from the default value.
Let us understand the function (default parameter values) with the below example. Save the program with defaultexample.scala name and run it from the terminal by using scala defaultexample.scala command(refer screenshot).
object defaultexample {
def main(args: Array[String]) {
println( "Reasult : " + additionepl() );
}
def additionepl( x:Int = 20, y:Int = 30 ) : Int = {
var output:Int = 0
output = x + y
return output
}
}
Output
Click Here To Download Code File.
4. Scala Higher-Order Functions
In Scala, a higher-order function takes other functions as parameters.
Let us understand the function (higher-order) with the below example which will take another function b and x value c and applies function b to c. Save the program with highorderexm.scala name and run it from the terminal by using scala highorderexm.scala command(refer screenshot).
object highorderexm {
def main(args: Array[String]) {
println( apply( output, 20) )
}
def apply(b: Int => String, c: Int) = b(c)
def output[X](z: X) = "[" + z.toString() + "]"
}
Output
Click Here To Download Code File.
5. Scala Nested Functions
In Scala, we can define a function inside a function and the function which is defined inside other functions is called local functions.
Let us understand nested functions with the below example to print a factorial calculator by calling a second method. Save the program with Nestedexample.scala name and run it from the terminal by using scala Nestedexample.scala command (refer screenshot).
object Nestedexample {
def main(args: Array[String]) {
println( fact(0) )
println( fact(1) )
println( fact(2) )
println( fact(3) )
println( fact(4) )
println( fact(5) )
println( fact(6) )
}
def fact(x: Int): Int = {
def factdata(x: Int, accum: Int): Int = {
if (x <= 1)
accum
else
factdata(x - 1, x * accum)
}
factdata(x, 1)
}
}
Output
Click Here To Download Code File.