Kotlin Lec 2 2020
Kotlin Lec 2 2020
Программирование на Kotlin
(p.2 - functions)
And after declaring it, you can use this function, as shown here:
printMultipleOfFive(10)
Function parameters
There are now two parameters inside the parentheses after the
function name: one named multiplier and the other named
andValue, both of type Int
Named arguments
• Sometimes it is helpful to use named
arguments when calling a function to make it
easier to understand the purpose of each
argument
printMultipleOf(multiplier = 4, andValue = 2)
printMultipleOf(4)
Inside the function, you use a return statement to return the value.
In this example, you return the product of the two parameters.
Using of Pair
• It’s also possible to return multiple values
through the use of Pairs:
fun multiplyAndDivide(number: Int, factor: Int): Pair<Int, Int> {
return Pair(number * factor, number / factor)
}
printResult(::add, 4, 2)
Writing good functions
• The best (easiest to use and understand)
functions do one simple task rather than trying to
do many.
• This makes them easier to mix and match and
assemble into more complex behaviors.
• Good functions also have a well defined set of
inputs that produce the same output every time.
• This makes them easier to reason about and test
in isolation.
Let’s code!
Questions?
Algorithms & Programming
Программирование на Kotlin
(p.2 - functions)