0
0
Kotlinprogramming~30 mins

Why functions are first-class in Kotlin - See It in Action

Choose your learning style9 modes available
Why functions are first-class in Kotlin
📖 Scenario: Imagine you are organizing a music playlist app. You want to create different ways to play songs, like playing normally, shuffling, or repeating. You will use functions as values to switch between these play modes easily.
🎯 Goal: Build a Kotlin program that shows how functions can be stored in variables, passed as arguments, and returned from other functions. This will demonstrate why functions are first-class citizens in Kotlin.
📋 What You'll Learn
Create a function variable that holds a function to play a song normally
Create a function variable that holds a function to play a song shuffled
Create a function that takes a function as a parameter and calls it
Create a function that returns a function to repeat a song
Call these functions and print the results
💡 Why This Matters
🌍 Real World
Using functions as values helps build flexible apps like music players, where you can change behavior easily without rewriting code.
💼 Career
Understanding first-class functions is key for Kotlin developers to write clean, reusable, and functional-style code in real projects.
Progress0 / 4 steps
1
Create function variables for play modes
Create two function variables called playNormal and playShuffle. Each should be a function that takes a String parameter called song and returns a String. playNormal should return the string "Playing {song} normally" using an f-string style. playShuffle should return "Playing {song} shuffled" similarly.
Kotlin
Need a hint?

Use lambda expressions to assign functions to variables. The syntax is val name: (ParameterType) -> ReturnType = { parameter -> expression }.

2
Create a function that takes a function parameter
Create a function called playSong that takes two parameters: a String called song and a function called playMode of type (String) -> String. The function should return the result of calling playMode(song).
Kotlin
Need a hint?

Define a function with a function parameter by specifying the parameter type as (String) -> String. Then call it inside the function.

3
Create a function that returns a function
Create a function called repeatPlay that takes no parameters and returns a function of type (String) -> String. The returned function should take a song string and return "Repeating {song}...".
Kotlin
Need a hint?

Return a lambda function from a function by specifying the return type as (String) -> String and returning a lambda.

4
Call the functions and print the results
Call playSong with the song "Hello" and the function playNormal. Then call playSong with the song "World" and the function playShuffle. Then call repeatPlay() to get a function, and call that function with the song "Hello". Print all three results each on its own line.
Kotlin
Need a hint?

Use println to print each result. Call repeatPlay() to get the function, then call it with a song.