Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM). It was developed by JetBrains and is now an official language for Android development. Kotlin is designed to be concise, expressive, and safe.
It is also interoperable with Java, which means that you can use Kotlin and Java code together in the same project.
Kotlin Functions & Classes (Lesson 11 PDF)
Overview about the Lesson
In this lesson, we’ll cover the basics of Kotlin Functions & Classes, including how to create and use functions, how to create classes and objects, and how to work with inheritance and polymorphism.







The Content Of The Lesson
The lesson is divided into the following sections:
- Creating and using functions
- Creating classes and objects
- Working with inheritance
- Working with polymorphism
- Using interfaces
- Using abstract classes
Why This Lesson
Functions & Classes are fundamental concepts in programming, and Kotlin provides excellent support for working with them. By the end of this lesson, you’ll have a solid understanding of Kotlin Functions & Classes and how to use them in your programs.
Kotlin Functions: Syntax and Examples
The PDF covers functions and an introduction to classes. This section concentrates on functions, which you will write in every Kotlin program. Classes are covered in depth in Lessons 12 and 13.
Declaring and calling a function
fun greet(name: String): String {
return "Hello, $name!"
}
fun main() {
println(greet("Laila")) // Hello, Laila!
}funstarts the declaration, followed by the name and parameters with their types.- The return type comes after the parentheses (
: String). - A function that returns nothing has the type
Unit, which you can omit.
Single-expression functions
fun square(x: Int) = x * x
fun isEven(n: Int) = n % 2 == 0
fun main() {
println(square(6)) // 36
println(isEven(7)) // false
}When the body is one expression, replace the braces and return with =. The return type is inferred.
Default and named arguments
fun price(amount: Double, taxRate: Double = 0.14, discount: Double = 0.0) =
amount * (1 + taxRate) - discount
fun main() {
println(price(100.0, 0.10)) // 110.00000000000001 (see note below)
println(price(100.0, discount = 5.0)) // uses default tax
}Default values mean you rarely need several overloaded versions of a function. Named arguments (discount = 5.0) make calls self-explanatory and let you skip parameters. (Floating-point results such as 110.00000000000001 are normal for Double; round for display.)
vararg: any number of arguments
fun total(vararg values: Int) = values.sum()
fun main() {
println(total(1, 2, 3, 4)) // 10
}Lambdas and higher-order functions
fun main() {
val double = { x: Int -> x * 2 }
println(double(21)) // 42
val nums = listOf(1, 2, 3, 4, 5)
println(nums.filter { it > 2 }.map { it * 10 }) // [30, 40, 50]
}A lambda is a function without a name. Functions such as filter and map take lambdas as arguments, and it is the implicit name of a single parameter.
Extension functions
fun String.shout() = uppercase() + "!"
fun main() {
println("kotlin".shout()) // KOTLIN!
}Extensions add functions to existing types without changing their source code.
Practice exercise
Write a function that returns the larger of two numbers, using a single-expression body.
fun maxOf2(a: Int, b: Int) = if (a > b) a else b
fun main() {
println(maxOf2(8, 13)) // 13
}Frequently Asked Questions
What is Unit in Kotlin?
Unit is the return type of functions that don’t return a meaningful value, similar to void in Java. You don’t need to write it.
Should parameters be val or var?
Function parameters are always read-only (like val). Copy a parameter into a local var if you need to change it.
Continue the course
← Previous: Lesson 10: For Loops & Ranges | Next: Lesson 12: Classes, Objects & Constructors →
- Lesson 1: Getting Started
- Lesson 2: Syntax & Output
- Lesson 3: Comments & Variables
- Lesson 4: Data Types
- Lesson 5: Operators
- Lesson 6: Set Operations & Grouping
- Lesson 7: Booleans & If…Else
- Lesson 8: When, While, Break & Continue
- Lesson 9: Arrays
- Lesson 10: For Loops & Ranges
- Lesson 11: Functions & Classes (you are here)
- Lesson 12: Classes, Objects & Constructors
- Lesson 13: Class Functions & Inheritance
Conclusion
In this blog post, we’ve covered the basics of Kotlin Functions & Classes, including how to create and use functions, how to create classes and objects, and how to work with inheritance and polymorphism.
We’ve also explored some advanced topics, such as using interfaces and abstract classes. With this knowledge, you’ll be well-equipped to work with Functions & Classes in your Kotlin programs.
Download From Link
You can download the lesson from the following link:












