-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Week1][입문편] 2강 코틀린에서 함수를 다루는 방법 (#5)
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
||
} |