Skip to content

Commit

Permalink
[Week1][입문편] 2강 코틀린에서 함수를 다루는 방법 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
yjsmk0902 committed Jun 26, 2024
1 parent d58df02 commit 687db7c
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
47 changes: 47 additions & 0 deletions 양승민/입문/src/lec08/lec08Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package lec08;

public class lec08Main {
public static void main(String[] args) {

// 4. 같은 타입의 여러 파라미터 받기 (가변인자)
String[] array = new String[]{"A", "B", "C"};
printAll(array);

printAll("A", "B", "C");
}

// 1. 함수 선언 문법
public int max(int a, int b) {
if (a > b) {
return a;
}
return b;
}

// 2. default parameter
public void repeat(String str, int num, boolean useNewLine) {
for (int i = 1; i <= num; i++) {
if (useNewLine) {
System.out.println(str);
} else {
System.out.print(str);
}
}
}

public void repeat(String str, int num) {
repeat(str, num, true);
}

public void repeat(String str) {
repeat(str, 3, true);
}

// 4. 같은 타입의 여러 파라미터 받기 (가변인자)
public static void printAll(String... strings) {
for (String str : strings) {
System.out.println(str);
}
}

}
38 changes: 38 additions & 0 deletions 양승민/입문/src/lec08/lec08Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package lec08

fun main() {

// 1. 함수 선언 문법
fun max(a: Int, b: Int) = if (a > b) a else b

// 2. default parameter
fun repeat(
str: String,
num: Int = 3,
useNewLine: Boolean = true
) {
for (i in 1..num) {
if (useNewLine) {
println(str)
} else {
print(str)
}
}
}

// 3. named argument
repeat("Hello world!", useNewLine = false)

// 4. 같은 타입의 여러 파라미터 받기 (가변인자)
fun printAll(vararg strings: String) {
for (str in strings) {
println(str)
}
}

val array = arrayOf("A", "B", "C")
printAll(*array)

printAll("A", "B", "C")

}

0 comments on commit 687db7c

Please sign in to comment.