Repetition and Functions – Day 89 of 100 Days…
Repeat statements are way easier than loops!
You can find more on this in the video at the bottom of the page.
Similar to loops, repeat statements are used to repeat a block of code a set number of times. However, repeat statements explicitly tell the Kotlin compiler how many times to repeat a block of code. (click below).
In this example, when the printBorder() function is invoked, it uses a repeat method to print a border of 23 equals signs (=) followed by a new line.
The repeat statement makes it easier to automate a single task. Simply pass the number of iterations and the Kotlin interpreter takes care of the rest.
Speaking of passing numbers, Kotlin has a similar syntax to Java when it comes to passing arguments to functions. The function is invoked using an argument, however, function headers are written a bit differently.
The function header is the beginning of a function. It is used to define how the function will accept arguments (called parameters). (Click below)
In this example, the function header uses the syntax parameterName: DataType to denote parameters. This differs from Java where the syntax is DataType parameterName.
It is worth mentioning that while assigning values to variables in Kotlin, you only need the value/expression and the val keyword. However, function headers require you to explicitly include data types for each parameter.
Lastly, it appears the Kotlin playground has finally updated to the latest version of Kotlin, 1.4.2 (click below).
It shouldn’t have a major effect on what I’m working on, but it’s worth noting that it’s up-to-date.
While I’ve enjoyed using the Kotlin playground, it’s time I set up Kotlin locally. Next week, I plan to livestream while I attempt to configure my local environment. I’ll post the official date on Twitter and LinkedIn so stay tuned!
Sources:
Android Basics: Introduction to Kotlin
Kotlin Playground
Kotlin logo image
Background acryllic artwork (top right)
Background painting (top left)