Functions
Learn how to create and call user-designed functions in your Kotlin programs!
StartKey Concepts
Review core concepts you need to learn to master this subject
Functions
Function Arguments
Default Arguements
Named Arguments
Return Statement
Single Expression Functions
Function Literals
Functions
Functions
fun greet() {
println("Hey there!")
}
fun main() {
// Function call
greet() // Prints: Hey there!
}
A function is a named, reusable block of code that can be called and executed throughout a program.
A function is declared with the fun
keyword, a function name, parentheses containing (optional) arguments, as well as an (optional) return type.
To call/invoke a function, write the name of the function followed by parentheses.
Functions
Lesson 1 of 1
- 1Now that we understand important programming concepts like control flow, collections, and loops, we can learn how to use functions to make our programs more efficient. A function is a reusable bl…
- 2In order to declare a function, we’ll need to understand the anatomy of a basic function. A function header contains valuable information about a function including its name, arguments, and its r…
- 4We can go further with arguments by naming them or setting default parameter values. To improve the readability of our code, we can name our arguments when invoking the function. For example, wit…
- 5The functions in the previous exercises produced output via println() statements contained within its code body; however, we also have the ability to return a value from a function. A _return sta…
- 6If one of our functions contains only a single expression, we can write the function out with shorthand syntax. This syntax allows us to create a function using only a single line of code. For exa…
- 7To simplify how we define functions, we can use function literals. A function literal is an unnamed function that can be treated as a value: we can call them, assign them as variables, pass them …
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory