본문 바로가기
Kotlin

함수

by Jinny96 2022. 7. 22.

기본 형태

fun 함수이름() : 리턴타입 {}


Example 1

fun main() {
  helloWorld() // Hello World
}

fun helloWorld() : Unit {
  println("Hello World")
}

Unit은 void로 생략해도 무관하다.


Example 2

fun main() {
  println(add(4, 5)) // 9
}

fun add(a : Int, b : Int) : Int {
  return a + b
}

파라미터 a와 b의 타입은 Int, 리턴 타입도 Int로 설정

'Kotlin' 카테고리의 다른 글

반복문  (0) 2022.07.22
ArrayList  (0) 2022.07.22
조건문  (0) 2022.07.22
String Template  (0) 2022.07.22
val vs var  (0) 2022.07.22

댓글