0% found this document useful (0 votes)
18 views12 pages

8 KFun

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views12 pages

8 KFun

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Kotlin Function

Types of Function
Kotlin standard library function
Kotlin user-dened function
Kotlin Default arguments

Ranjit Patnaik Kotlin Functions 1 / 12


Kotlin Function
Types of Function
Kotlin standard library function
Kotlin user-dened function
Kotlin Default arguments

Content

1 Kotlin Function
2 Types of Function
3 Kotlin standard library function
4 Kotlin user-dened function
Creating user-dened function
Calling of user-dened function
5 Kotlin Default arguments
No arguments are passed while calling a function

Ranjit Patnaik Kotlin Functions 2 / 12


Kotlin Function
Types of Function
Kotlin standard library function
Kotlin user-dened function
Kotlin Default arguments

Kotlin Function

A function is a unit of code that performs a special task. In programming,


function is used to break the code into smaller modules which makes the
program more manageable.
For example: If we have to compute sum of two numbers then dene a fun
sum().
fun sum(a: Int, b: Int): Int {
return a + b
}

We can call sum(x, y) at any number of times and it will return sum of two numbers. So,
function avoids the repetition of code and makes the code more reusable.

Ranjit Patnaik Kotlin Functions 3 / 12


Kotlin Function
Types of Function
Kotlin standard library function
Kotlin user-dened function
Kotlin Default arguments

Types of Function

In Kotlin, there are two types of function-


Standard library function
User dened function

Ranjit Patnaik Kotlin Functions 4 / 12


Kotlin Function
Types of Function
Kotlin standard library function
Kotlin user-dened function
Kotlin Default arguments

Kotlin standard library function

In Kotlin, there are dierent number of built-in functions already


dened in standard library and available for use. We can call them by
passing arguments according to requirement.
In below program, we will use built-in functions arrayOf(), sum() and
println(). The function arrayOf() require some arguments like
integers, double etc to create an array and we can nd the sum of all
elements using sum() which does not require any argument.

Ranjit Patnaik Kotlin Functions 5 / 12


Kotlin Function
Types of Function
Kotlin standard library function
Kotlin user-dened function
Kotlin Default arguments

Continue....

fun main(args: Array<String>) {


var sum = arrayOf(1,2,3,4,5,6,7,8,9,10).sum()

println("The sum of all the elements of an array is: $sum")


}

The list of dierent standard library functions and their use-


sqrt()  Used to calculate the square root of a number.
print()  Used to print a message to standard output.
rem()  To nd the remainder of one number when divided by another.
toInt()  To convert a number to integer value.
readline()  Used for standard input.
compareTo()  To compare two numbers and return boolean value.

Ranjit Patnaik Kotlin Functions 6 / 12


Kotlin Function
Types of Function
Kotlin standard library function
Kotlin user-dened function
Kotlin Default arguments

Kotlin user-dened function

A function which is dened by the user is called user-dened function. As


we know, to divide a large program in small modules we need to dene
function. Each dened function has its own properties like name of
function, return type of a function, number of parameters passed to the
function etc.

Ranjit Patnaik Kotlin Functions 7 / 12


Kotlin Function
Types of Function
Kotlin standard library function
Kotlin user-dened function
Kotlin Default arguments

Creating user-dened function


In Kotlin, function can be declared at the top, and no need to create a
class to hold a function, which we are used to do in other languages such
as Java or Scala.
Generally we dene a function as:
fun fun_name(a: data_type, b: data_type, ......): return_type {
// other codes
return
}

Explanation of the above code:


fun Keyword to dene a function.
fun_name  Name of the function which later used to call the function.
a: data_type  Here, a is an argument passed and data_type specify the data type of
argument like integer or string.
return_type  Specify the type of data value return by the function.
{. . . .}  Curly braces represent the block of function.
Ranjit Patnaik Kotlin Functions 8 / 12
Kotlin Function
Types of Function
Kotlin standard library function
Kotlin user-dened function
Kotlin Default arguments

Continue....

Kotlin function mul() to multiply two numbers having same type of


parameters-
fun mul(num1: Int, num2: Int): Int {
var number = num1.times(num2)
return number
}

Explanation: We have dened a function above starting with fun keyword whose return type is
an Integer.

Ranjit Patnaik Kotlin Functions 9 / 12


Kotlin Function
Types of Function
Kotlin standard library function
Kotlin user-dened function
Kotlin Default arguments

Calling of user-dened function

We create a function to assign a specic task. Whenever a function is


called the program leaves the current section of code and begins to execute
the function.
The ow-control of a function-
1 The program comes to the line containing a function call.

2 When function is called, control transfers to that function.

3 Executes all the instruction of function one by one.

4 Control is transferred back only when the function reaches closing

braces or there any return statement.


5 Any data returned by the function is used in place of the function in

the original line of code.

Ranjit Patnaik Kotlin Functions 10 / 12


Kotlin Function
Types of Function
Kotlin standard library function
Kotlin user-dened function
Kotlin Default arguments

Kotlin | Default and Named argument

The arguments which need not specify explicitly while calling a function are
called default arguments. If the function is called without passing
arguments then the default arguments are used as function parameters. In
other cases, if arguments are passed during a function call then passed
arguments are used as function parameters.
There are three cases for default arguments-
1 No arguments are passed while calling a function

2 Partial arguments are passed while calling a function

3 All arguments are passed while calling a function

Ranjit Patnaik Kotlin Functions 11 / 12


Kotlin Function
Types of Function
Kotlin standard library function
Kotlin user-dened function
Kotlin Default arguments

No arguments are passed while calling a function


When no argument is passed while calling a function then the default
arguments are used as function parameters. We need to initialize the
variables while dening a function. Kotlin program of calling student()
function without passing an arguments-
// default arguments in function definition name, standard and roll_no
fun student(name: String="Praveen", standard: String="IX" , roll_no: Int=11) {
println("Name of the student is: $name")
println("Standard of the student is: $standard")
println("Roll no of the student is: $roll_no")
}
fun main(args: Array<String>) {
val name_of_student = "Gaurav"
val standard_of_student = "VIII"
val roll_no_of_student = 25
student() // passing no arguments while calling student
}

Ranjit Patnaik Kotlin Functions 12 / 12

You might also like