-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScalaDef1.scala
68 lines (55 loc) · 1.8 KB
/
ScalaDef1.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package scalademo
/* Syntax of a function:
*
* def functionName ([list of parameters]) : [return type] = {
* function body
* return [expr]
* }
*
*/
object Sample {
def example1() { // a function without = operator and no parameter
println("this is an example function")
}
def example2() = { // a function with = operator but no parameter
var a = 10
a // return type inferred by compiler from last expression/statement in the function
}
def add1(a: Int, b: Int) = {
var c = a + b
println(c)
}
def add(a: Int, b: Int): Int = { // with params and return type
var sum: Int = 0
sum = a + b
return sum
}
def voidMethod(): Unit = { // Unit is equivalent to 'void'
println("Hello, Scala!")
}
def defaultValues(a: Int = 10, b: Int = 0): Int = { // parameters with default values as 0
a + b
}
def namedParamereters(firstNumber: Int, secondNumber: Int): Int = {
firstNumber + secondNumber
}
}
object ScalaDef1 {
def main(args: Array[String]): Unit = {
Sample.example1()
println(Sample.example2())
println(Sample.add(15, 25))
Sample.voidMethod
Sample.add1(100, 200)
println(Sample.defaultValues()) // shows output with default values
println(Sample.defaultValues(11)) // first value has been replaced with 11
println(Sample.defaultValues(11, 22)) // both values overridden the default values
// namedParameter examples:
var result1 = Sample.namedParamereters(firstNumber = 15, secondNumber = 2) // Parameters names are passed during call
var result2 = Sample.namedParamereters(secondNumber = 15, firstNumber = 2) // Parameters order have changed during call
var result3 = Sample.namedParamereters(15, 2) // Only values are passed during call
println(result1)
println(result2)
println(result3)
}
}