Key Concepts

Review core concepts you need to learn to master this subject

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
  1. 1
    Now 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…
  2. 2
    In 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…
  3. 3
    Arguments are pieces of data we can feed to our functions in order to produce dynamic results. We can include as many arguments as the function needs. Think of the println() function. When we use …
  4. 4
    We 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…
  5. 5
    The 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…
  6. 6
    If 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…
  7. 7
    To 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 …
  8. 8
    Congratulations on completing this lesson. Let’s go over what we learned: * A function is a reusable block of code that can be invoked throughout a program. * In order to execute a function, it mu…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo