Amandeep Singh
Posted on September 4, 2022
Functions are something that we have used already for example the main()
function. Functions are used to reuse code very easily. But today we will learn how to create our very own functions.
About Functions:
- A block of code that performs a specific task
- Breaks a large program into smaller modular chunks
- Can take arguments with either named or default values
- Functions are declared using the
fun
keyword and their general syntax is
fun myFunction(parameter1: Int, parameter2: String){
}
Parameters: Function parameters are defined using Pascal notation i.e., First the name of the parameter (example parameter1) followed by the Datatype of the parameter (example: Int). Parameters are separated using commas, and each parameter must be explicitly typed.
Function Arguments
Default Parameters: Function parameters can have default values, which are used when you skip the corresponding argument. This reduces the number of overloads.
Default values provide a fallback if no parameter value is passed.
Required Parameters: If no default is specified for a parameter, the corresponding argument is required.
Default vs Required parameters
Named Arguments
When calling a function, you can name one or more of its arguments. This can be helpful when a function has many arguments and it's difficult to associate a value with an argument, especially if it's a boolean or null value.
When you use named arguments in a function call, you can freely change the order they are listed in, and if you want to use their default values, you can just leave these arguments out altogether.
Here we have a function reformat() which has 4 arguments with default values.
fun reformat(
str: String,
normalizeCase: Boolean = true,
upperCaseFirstLetter: Boolean = true,
divideByCamelHumps: Boolean = false,
wordSeparator: Char = ' ',
) {
//Your code
}
It's considered good style to put default arguments after positional arguments, that way callers only have to specify the required arguments.
The advantage of named functions is that we don’t have to name all its arguments when calling the function. you can skip all the default values and call the function like this:
reformat("This is a long String!")
Single-Expressed functions: Kotlin single-expression function is a function where a single expression is assigned to the function, and the expression’s evaluated value is returned when this function is called .
Single-expression functions are compact functions that make your code more concise and readable.
The syntax of Kotlin Single-Expression Function is:
fun functionName(parameters) = expression
An example of single-expressed of function:
fun double(x: Int):Int = x * 2 //single-expressed function
fun main(){
val x =2
val square = double(x)
println("The square of $x is $square")
}
OUTPUT: The square of 2 is 4
That's it for this Blog. Now you have a tight grip over Kotlin functions. We almost covered functions is Kotlin except Lambdas and higher-order functions
, which we will cover in the next blog
If you have doubt in any part you can ask it in the discussion section.
Wanna connect? connect with me on LinkedIn
Posted on September 4, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.