-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.kt
42 lines (29 loc) · 1.06 KB
/
Functions.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
/**
* a function is a sequence of instructions grouped together to perform a specific task
* It can be invoked by calling its name
* It is an expression that has a body, parameters and a return value
*/
fun main(){
/** declaring a function
//p1 and p2 are parameters of same or diff types (Int,bool,...) there can be one or many parameters
fun functionName(p1: Type1, p2, type2, ...) :ReturnType{
body //statements which define a function
return result
}
*/
/**
* defining a simple function that calculates the sum of integer numbers and returns it
NB: The arguments of a function can be accessed only in the function
*/
fun sum(a: Int, b: Int) : Int{
return a + b
}
println(sum(2,4))
/**
* Single expression functions return a single expression
* These functions can be written without curly braces
*/
fun sum1(x: Int, y: Int) : Int = x + y //return type specified explicitly
fun sum3(i: Int, j: Int) = i + j //return type not specified
println(sum1(34,22))
}