# Scala – Functions

```// Functions in Scala
object Demo {

object Math {
def add(x: Int, y: Int): Int = {
return x + y;
}
def square(x: Int) = x*x;
}
def add(x: Int, y: Int): Int = {
return x + y;
}
def subtract(x: Int, y: Int): Int = {
x - y;
}
def multiply(x: Int, y: Int): Int = x * y;

def divide(x: Int, y: Int) = x / y;

def main(args: Array[String]) {
println(Math square 3);
println(subtract(45, 15));
println(multiply(45, 15));
println(divide(45, 15));
}
}

/*
output:
60
9
60
30
675
3

*/```
```// Functions in Scala
object Demo {

object Math {
def +(x: Int, y: Int): Int = {
return x + y;
}
def **(x: Int) = x*x;
}

def print(x: Int, y : Int): Unit = {
println(x+y);
}

def main(args: Array[String]) {

var add = (x : Int, y : Int) => x + y;
val sum = 10 * 20;
print(100, 200);
println(Math.+(60, 15));
println(Math ** 3);
}
}

/*
OUTPUT:
800
300
75
9

*/
```

```// Scala - Higher-Order Functions
object Demo {

def math(x: Double, y:Double, z:Double, f: (Double, Double)=>Double): Double = f(f(x, y), z);

def main(args: Array[String]) {
val result = math(50, 20, 10, _ max _);
println(result);
}
}

```
```// Scala - Partially Applied Functions
import java.util.Date
object Demo {

def log(date : Date, message: String) = {
println(date + "   " + message);
}

def main(args: Array[String]) {
val sum = (a: Int, b: Int, c: Int) => a + b + c
val f = sum(10, _: Int, _ : Int)
println(f(100, 200));

val date = new Date;
val newLog = log(date, _ :String);
newLog("The message 1");
newLog("The message 2");
newLog("The message 3");
newLog("The message 4");

}
}

/*
OUTPUT:
310
Fri Dec 15 18:28:45 CET 2017   The message 1
Fri Dec 15 18:28:45 CET 2017   The message 2
Fri Dec 15 18:28:45 CET 2017   The message 3
Fri Dec 15 18:28:45 CET 2017   The message 4

*/
```