Learn Kotlin: Functions
You have already used functions like print and println in hello world lesson. Those are called predefined standard library. There is also user defined functions which is the functions you made yourself.
In this lesson we’re going to talk about more about functions. This is going to be fun!
1. Definition — What is a function?
Function is a named sub-program which performs a specific task. We need to call the function by name to execute the sub-program. You can think of a function as a machine.
Functions takes inputs and process it into outputs. Inputs in a function are called parameters and output is the return value. We will talk more about parameters and return value later. All you need to know is that function is like mini machine in your program which responsible to do certain things like processing data.
Kotlin functions can be declared at top level in a file. Different from methods in Java which you have to create a class to hold them.
2. The function of Functions — Why do we write functions?
Functions are used because of following reasons :
- Functions improve the readability of code. By giving names to functions, it’s easier to read code because we know what each sub-program is for.
- Functions improve the reusability of the code. Functions can be called more than once, so you can and should use a function to solve specific tasks without having to keep repeating writing same code. Remember DRY, don’t repeat yourself.
- Functions make it easier to trace errors thus faster debugging process.
3. Function Definition Syntax — How to define a function?
Let’s see how we define a function in Kotlin. Here is the base syntax to define a function :
Here is the example :
In the above example, getGreeting() function takes a parameter which has type of String and returns a value also of type String.
To return a value means to output. Just like in the machine illustration before, the machine takes inputs and process them into output. In a funtion, the inputs are parameters and the output is the return value.
The values which you pass when you call a function for the parameters are called arguments. You have to provide arguments with the right data types, which means if you define a function which has a parameter of string, you must pass a string value when calling the function.
You can also define a method which returns no value at all. This function will automatically return Unit type. Unit type is basically equivalent to “nothing useful”. Funtion which returns no value at all don’t need return type in the definition.
4. Function naming convention
Just like naming a variable, naming a function has some convention which you should follow :
- Use camelCase. camelCase is the way to write phrases without spaces or punctuation, and for multiple word phrases we separate them by capitalizing the first character of each word.
- Use verbs which describe what your function does (e.g. calculateTotal, sendMessage, stop, getName, setColor).
- Function names should be as short as possible so that it’s simple to type and easy to remember.
5. Single-expression Functions
When a function only has a single expression on its body, the curly braces can be omitted and the body is specified after a “=” symbol. You also can ommit the return type since the compiler can infer it from the expression.
All right! That’s it for Kotlin functions. You can save this article for your future reference. Thank you so much for reading. If you like these articles, let me know in the comment section below. You can also give suggestion, I’m open for any kind of feedback.
— Learn Kotlin Series —
- Learn Kotlin: Introduction
- Learn Kotlin: Hello World!
- Learn Kotlin: Variables
- Learn Kotlin: Data Types
- Learn Kotlin: Basic Operators
- Learn Kotlin: Functions (this article)
— Dictionary —
- Debugging : the process of finding and resolving bugs within program.
- DRY (Don’t Repeat Yourself) : a principle of software development aimed at reducing the repetition of code. You should extract out the codes that are common for the application, and place them at a single place and reuse them instead of repeating it.