1. Defining Packages
패키지의 정의는 소스파일의 맨 위에 있어야 한다.
package my.demo import java.util.* // ... |
패키지와 소스 디렉토리가 정확하게 일치할 필요는 없다.
2. Defining functions
function1: 두개의 Int 파라미터와 하나의 Int 리턴 함수의 정의
1 2 3 4 5 6 7 8 | fun sum(a: Int, b: Int): Int { return a + b } fun main(args: Array<String>) { print("sum of 3 and 5 is ") println(sum(3, 5)) } | cs |
function2: 표현식과 암시적 리턴값이 같이 있는 함수의 정의
1 2 3 4 5 | fun sum(a: Int, b: Int) = a + b fun main(args: Array<String>) { println("sum of 19 and 23 is ${sum(19, 23)}") } | cs |
function3: 리턴값이 없는 함수의 정의
1 2 3 4 5 6 7 | fun printSum(a: Int, b: Int): Unit { println("sum of $a and $b is ${a + b}") } fun main(args: Array<String>) { printSum(-1, 8) } | cs |
** Unit는 생략가능.
1 2 3 4 5 6 7 | fun printSum(a: Int, b: Int) { println("sum of $a and $b is ${a + b}") } fun main(args: Array<String>) { printSum(-1, 8) } | cs |
'Kotlin > Basic Syntax' 카테고리의 다른 글
6. Using for loop & while loop & when expression (0) | 2018.05.31 |
---|---|
5. Using Type checks & Automatic casts (0) | 2018.05.31 |
4. Using nullable values and Checking for null (0) | 2018.05.31 |
3. Using String templates & Conditional expressions (0) | 2018.05.31 |
2. Defining Variables & Comments (0) | 2018.05.31 |