# Kotlin

> I made these notes using the [Developer Android Course](https://developer.android.com/courses)

* We can use the Kotlin playground [here](https://developer.android.com/training/kotlinplayground) to practice.

## Hello World in Kotlin

```kotlin
fun main() {
    println("Hello, World!")
}
```

## Style guide

* Function names should be in camel case and should be verbs or verb phrases.
* Each statement should be on its own line.
* The opening curly brace should appear at the end of the line where the function begins.
* There should be a space before the opening curly brace.
* The function body should be indented in by 4 spaces. Do not use a tab character to indent your code, type in 4 spaces.
* The closing curly brace is on its own line after the last line of code in the function body. The closing brace should line up with the fun keyword at the beginning of the function.
* See the full Kotlin style guide [here](https://developer.android.com/kotlin/style-guide)

## Variables

### Data Types

![data types](https://1679624655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEkk28J0B2BeDMuesRMr1%2Fuploads%2Fgit-blob-cb57bff223b2b61c9e362dab31303c9e2cafef5d%2F2023-03-25-13-42-19.png?alt=media)

> Source Developer Android

* More about Double [here](https://kotlinlang.org/docs/numbers.html#floating-point-types)
* More about variables [here](https://play.kotlinlang.org/byExample/01_introduction/03_Variables)
* [Basic types](https://kotlinlang.org/docs/basic-types.html)
* [String templates](https://kotlinlang.org/docs/basic-syntax.html#string-templates)
* [Keywords and operators](https://kotlinlang.org/docs/keyword-reference.html)
* [Basic syntax](https://kotlinlang.org/docs/basic-syntax.html)

### Declaration

![declaration](https://1679624655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEkk28J0B2BeDMuesRMr1%2Fuploads%2Fgit-blob-cd0f9797b4e3f61a870b8bbf3b0a0ca746fd24a6%2F2023-03-25-13-45-03.png?alt=media)

> Source Developer Android

* Variables that can be reassigned use the var keyword.

```kotlin
var x = 5 // `Int` type is inferred
```

* Constants have to be declared using `val`
* val keyword - Use when you expect the variable value will not change.
* var keyword - Use when you expect the variable value can change.
* Constants in Kotlin [here](https://developer.android.com/kotlin/style-guide#constant_names)

## Comments

```kotlin
// this is a comment
/* 
* This is a multiple
* lines
* comment
*/
```

## Functions

![function syntax](https://1679624655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEkk28J0B2BeDMuesRMr1%2Fuploads%2Fgit-blob-125a33053f7b268fdafd0318c58452abdc83876c%2F2023-03-25-16-08-23.png?alt=media)

### Function declaration

![Declare function](https://1679624655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEkk28J0B2BeDMuesRMr1%2Fuploads%2Fgit-blob-17025be72d0b691fff8e8b8b38dfff017aa06512%2F2023-03-25-15-18-30.png?alt=media)

> Source Developer Android

* Example

```kotlin
fun birthdayGreeting(): String {
    println("Happy Birthday, Rover!")
    println("You are now 5 years old!")
}
```

### Declaring a function with a return type

![Declare fun with return](https://1679624655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEkk28J0B2BeDMuesRMr1%2Fuploads%2Fgit-blob-5e084efb77d13786bb893e1c162b740596998d02%2F2023-03-25-15-25-47.png?alt=media)

> Source Developer Android

* Example

```kotlin
fun birthdayGreeting(): String {
    val nameGreeting = "Happy Birthday, Rover!"
    val ageGreeting = "You are now 5 years old!"
    return "$nameGreeting\n$ageGreeting"
}
// call it
birthdayGreeting()
```

### The Unit type

By default, if you don't specify a return type, the default return type is Unit. Unit means the function doesn't return a value. Unit is equivalent to void return types in other languages

### Store a function in a var

```kotlin
fun main() {
    val greeting = birthdayGreeting()
}
```

### Function with parameter(s)

* Declaration

![declare a function with a parameter](https://1679624655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEkk28J0B2BeDMuesRMr1%2Fuploads%2Fgit-blob-1b6d98f2901cd3f6a75719beae0f04b06379bd75%2F2023-03-25-15-42-52.png?alt=media)

> Source Developer Android

* Example with one parameter

```kotlin
fun birthdayGreeting(name: String): String {
    val nameGreeting = "Happy Birthday, $name!"
    val ageGreeting = "You are now 5 years old!"
    return "$nameGreeting\n$ageGreeting"
}
// call it and print it
println(birthdayGreeting("Rex"))
```

* Example with multiple parameters

```kotlin
fun birthdayGreeting(name: String, age: Int): String {
    val nameGreeting = "Happy Birthday, $name!"
    val ageGreeting = "You are now $age years old!"
    return "$nameGreeting\n$ageGreeting"
}
// call it and print it
println(birthdayGreeting("Rex", 2))
// or
println(birthdayGreeting(name = "Rex", age = 2))
// or
println(birthdayGreeting(age = 2, name = "Rex"))
```

> :warning: Warning: Unlike in some languages, such as Java, where a function can change the value passed into a parameter, parameters in Kotlin are immutable. You cannot reassign the value of a parameter from within the function body.

### Function with default arguments

* Example

```kotlin
fun birthdayGreeting(name: String = "Rover", age: Int): String {
    return "Happy Birthday, $name! You are now $age years old!"
}
// multiple ways to print it and call it
println(birthdayGreeting(age = 5))
println(birthdayGreeting("Rex", 2))
println(birthdayGreeting(age = 5))
println(birthdayGreeting(age = 2))
```

## Resources

{% embed url="<https://developer.android.com/courses>" %}
Official Android Training courses
{% endembed %}

{% embed url="<https://kotlinlang.org/docs>" %}
Kotlin Documentation
{% endembed %}

{% embed url="<https://www.w3schools.com/KOTLIN/index.php>" %}
W3Schools Kotlin Tutorial
{% endembed %}
