Functions in Kotlin
Tags: functions, beginners, android, kotlin
Published: 2022-09-04 05:48:06


![Functions in Kotlin By Amandeep](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zrodzieg3w6anulwmmr1.png)


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](#Default Parameters)
- [Required Parameters](#Required Parameters)
- [Named arguments](#Named arguments)
- [Single-expressed functions](#Single-expressed)

**Default Parameters:** <a name = "Default Parameters"></a> 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. 

![Default Parameters](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/routbxvi7uaixi0hd7um.png)  

**Required Parameters:**<a name = "Required Parameters" /> If no default is specified for a parameter, the corresponding argument is required.


![Required Parameters](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/et9iy9axjpv1zwnkfjoo.png)

### Default vs Required parameters 

![Default versus required parameters
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr7iu19c8vrfdj4eoo5y.png)

### Named Arguments <a name = "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:** <a name = "Single-expressed" /> 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](https://www.linkedin.com/in/amandeep-singh-452949246/)


![Thank You Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p4rt65w2vb1o4dx7ccqu.png)