0
0
Kotlinprogramming~15 mins

Single-expression functions in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Single-expression functions
📖 Scenario: You are building a simple calculator app that needs to perform basic math operations quickly and clearly.
🎯 Goal: Create single-expression functions in Kotlin to perform addition, subtraction, multiplication, and division.
📋 What You'll Learn
Create single-expression functions for addition, subtraction, multiplication, and division
Use concise syntax without curly braces or return statements
Call each function with example inputs
Print the results of each function call
💡 Why This Matters
🌍 Real World
Single-expression functions help write clean and simple code for small tasks like calculations in apps.
💼 Career
Understanding concise function syntax is useful for Kotlin developers to write readable and maintainable code efficiently.
Progress0 / 4 steps
1
Create addition function
Create a single-expression function called add that takes two Int parameters named a and b and returns their sum.
Kotlin
Need a hint?

Use the equals sign = to define a single-expression function without curly braces.

2
Create subtraction function
Create a single-expression function called subtract that takes two Int parameters named a and b and returns the result of a - b.
Kotlin
Need a hint?

Follow the same pattern as the add function but use subtraction.

3
Create multiplication and division functions
Create two single-expression functions: multiply and divide. Both take two Int parameters named a and b. multiply returns a * b. divide returns a / b.
Kotlin
Need a hint?

Use the same single-expression function style for multiplication and division.

4
Print results of function calls
Call each function with the arguments 10 and 5. Print the results using println with messages: "10 + 5 = result", "10 - 5 = result", "10 * 5 = result", and "10 / 5 = result".
Kotlin
Need a hint?

Use string templates with ${} inside println to show results.