-
Notifications
You must be signed in to change notification settings - Fork 0
/
KotlinFunctions.kt
62 lines (51 loc) · 1.42 KB
/
KotlinFunctions.kt
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
package com.example.confuste.kotlinexamples
/**
* Created by Alejandro Fuster on 29/11/17.
*/
class KotlinFunctions {
init{
//Nothing to initialize
}
/**
* Calculate a sum
* @param a: Integer
* @param b: Integer
* @return Int number which is the final result
*/
fun sumSingleLine (a:Int, b: Int) : Int = a+b
/**
* Calculate a sum
* @param a: Integer
* @param b: Integer
* @return Int number which is the final result
*/
fun sumSeveralLines(a:Int, b:Int): Int {
return a+b
}
/**
* Anonymous function without name. It must be assigned to a variable.
* This function only support a single line.
*/
val sumAnonymousSingleLine = { x: Int, y: Int -> x + y }
/**
* Anonymous function without name. It must be assigned to a variable.
* This function supports several lines.
*/
val sumAnonymousSeveralLines = fun(a: Int, b: Int): Int {
return a + b
}
/**
* Calculate an operation between 2 numbers
* @param a: Integer
* @param b: Integer
* @param c: Function which require 2 Int and return 1 Int.
* @return Int
*/
fun sumHigherOrder(a: Int, b: Int, c: (Int, Int) -> Int ) : Int{
return c(a, b)
}
fun exampleUseOfsumHigherOrder() {
sumHigherOrder(3, 2, sumAnonymousSingleLine)
sumHigherOrder(3, 2, sumAnonymousSeveralLines)
}
}