0
0
Kotlinprogramming~10 mins

Function declaration syntax in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a function named greet.

Kotlin
fun [1]() {
    println("Hello!")
}
Drag options to blanks, or click blank then click option'
AsayHello
BprintMessage
Chello
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than requested.
Forgetting to write the function name after fun.
2fill in blank
medium

Complete the code to declare a function that returns an Int value 5.

Kotlin
fun getNumber(): [1] {
    return 5
}
Drag options to blanks, or click blank then click option'
AString
BDouble
CInt
DBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using String or other types instead of Int.
Omitting the return type when the function returns a value.
3fill in blank
hard

Fix the error in the function declaration to accept a String parameter named name.

Kotlin
fun greet([1]: String) {
    println("Hello, $name!")
}
Drag options to blanks, or click blank then click option'
Agreeting
Bname
CName
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than used inside the function.
Capitalizing the parameter name incorrectly.
4fill in blank
hard

Fill both blanks to declare a function that takes two Int parameters and returns their sum.

Kotlin
fun add([1]: Int, [2]: Int): Int {
    return a + b
}
Drag options to blanks, or click blank then click option'
Aa
Bx
Cb
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Using parameter names that don't match the return statement.
Swapping the order of parameters.
5fill in blank
hard

Fill all three blanks to declare a function that returns a greeting message with the given name.

Kotlin
fun greet([1]: String): [2] {
    return "Hello, " + [3]
}
Drag options to blanks, or click blank then click option'
Aname
BString
DInt
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter or return types.
Using a different variable name than the parameter.