Introduction to Functions in Kotlin

Welcome aboard our Kotlin programming adventure! Today, we're delving into functions. In the coding universe, a function serves as a mini-program performing a particular task. It helps to partition more extensive programs into organized, reusable components. Envision functions as home robots, each responsible for a specific task.

Writing a Basic Function in Kotlin

To write a Kotlin function, we define its name and task using this syntax:

Here, the keyword fun declares a function. Let's define a function named greet that extends a friendly message to the world:

This function, named greet, prints "Hello, World!" to the console when invoked.

Invoking or Calling a Function in Kotlin

To execute its task, we "call" or "invoke" a function using its name followed by parentheses. Functions are often summoned within the special main function. The main function acts as the concertmaster of our program, steering when and which parts (functions) of our code perform.

Here's an example of main calling :

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal