2
2
.
.
5
5
.
.
2
2
F
F
u
u
n
n
c
c
t
t
i
i
o
o
n
n
D
D
e
e
c
c
l
l
a
a
r
r
a
a
t
t
i
i
o
o
n
n
-
-
A
A
n
n
o
o
n
n
y
y
m
m
o
o
u
u
s
s
I
I
n
n
f
f
o
o
[
[
R
R
]
]
Anonymous Function Declaration declares Anonymous Function (Function that has a Name).
This is alternative to Lambda Expressions which also declares Anonymous Function and it was introduced because
return keyword returns from the nearest function declared with the fun keyword
so if return is used inside Lambda it will return from the enclosing function
and to return just from Lambda requires the return@label syntax (which is somewhat clunky)
to avoid clunkiness Anonymous Function Declarations allows you to use return to return just from the Function
A secondary reason is that it's indeed impossible to fit the return type declaration into the syntax of a lambda.
Anonymous Function Declarations is used in the same way as Named Function Declarations except that
you avoid specifying name
just like with Lambda Expressions you either (which means you can't call it with named Parameter either)
save Anonymous Function in a Variable
or declare it directly at the place of Function Input Parameter (so that it gets stored in that Parameter)
supports both Block and Assignment Syntax
you can specify Data Type of Return Value
Content
Syntax - Block
Syntax - Assignment
Function as Input Parameter
Function as Return Value
Syntax
//ANONYMOUS FUNCTION DECLARATIONS.
fun (name:String, age:Int) : (String) { return("$name is ${age} years old.") } //Block Syntax
fun (name:String, age:Int) : (String) = "$name is ${age} years old." //Assignment Syntax
//CALL FUNCTION THROUGH VARIABLE.
greet("John", 50) //No Default Values or Named Parameters
S
S
y
y
n
n
t
t
a
a
x
x
-
-
B
B
l
l
o
o
c
c
k
k
Function Declaration
uses Keyword fun fun
followed by Function's Signature (name:String, age:Int) : (String) (accepts 2 Parameters & returns String)
followed by Function's Body { return("$name is $age years old.") } (required return Keyword)
Block Syntax
//DECLARE VARIABLE THAT CAN HOLD FUNCTION DATA TYPE (OF SPECIFIED SIGNATURE).
var greet: (String, Int) -> (String) //Closure that accepts 2 Parameters & returns String
//DECLARE & IMPLEMENT FUNCTION.
greet = fun (name:String, age:Int) : (String) { return("$name is $age years old.") }
//CALL FUNCTION.
var result = greet ("John", 50)
//DISPLAY RESULT.
print(result)
S
S
y
y
n
n
t
t
a
a
x
x
-
-
A
A
s
s
s
s
i
i
g
g
n
n
m
m
e
e
n
n
t
t
Function is declared by
using Keyword fun fun
followed by Function's Signature (name:String, age:Int) : (String) (accepts 2 Parameters & returns String)
followed by equal sign "=" "="
followed by single statement "$name is $age years old." (forbidden return Keyword)
Assignment Syntax
//DECLARE VARIABLE THAT CAN HOLD FUNCTION DATA TYPE (OF SPECIFIED SIGNATURE).
var greet: (String, Int) -> (String) //Closure that accepts 2 Parameters & returns String
//DECLARE & IMPLEMENT FUNCTION.
greet = fun (name:String, age:Int) : (String) = "$name is $age years old."
//CALL FUNCTION.
var result = greet("John", 50)
//DISPLAY RESULT.
print(result)
F
F
u
u
n
n
c
c
t
t
i
i
o
o
n
n
a
a
s
s
I
I
n
n
p
p
u
u
t
t
P
P
a
a
r
r
a
a
m
m
e
e
t
t
e
e
r
r
When you compare this code with Lambda Expressions as Function Parameter you see that it is as compact while
allowing us to specify Data Type of Return Value
use normal return
use Syntax that is similar to Named Function Declarations (unlike Lambda that uses completely different Syntax)
Lambda Expressions as Function Parameter
executeFunction({ name: String, age: Int -> "$name IS $age YEARS OLD." })
Function as Input Parameter
//===========================================================================================================
// FUNCTION: executeFunction
//===========================================================================================================
fun executeFunction(receivedFunction: (name: String, age: Int) -> String) {
var result = receivedFunction("John", 20)
println(result)
}
//===========================================================================================================
// FUNCTION: main
//===========================================================================================================
fun main() {
//DECLARE LOCAL VARIABLE.
var weight = 150
//DECLARE VARIABLE TO HOLD FUNCTION DATA TYPE.
var anonymousFunction : (name: String, age: Int) -> String
//DECLARE ANONYMOUS FUNCTION USING LAMBDA EXPRESSION.
anonymousFunction = fun (name: String, age: Int) : String {return "$name is $age years old and $weight
kg."}
//CALL FUNCTION WITH FUNCTION AS PARAMETER (SENDS CLOSURE WITH weight Property).
executeFunction(anonymousFunction)
//USE LAMBDA EXPRESSION DIRECTLY AS PARAMETER (SENDS CLOSURE WITH weight Property).
executeFunction(fun (name: String, age: Int) : String { return "$name is $age years old and $weight kg." })
}
F
F
u
u
n
n
c
c
t
t
i
i
o
o
n
n
a
a
s
s
R
R
e
e
t
t
u
u
r
r
n
n
V
V
a
a
l
l
u
u
e
e
Function getFunction() actually returns a Closure with
this Anonymous Function as its Method
Property weight which is declared outside of the Scope of Anonymous Function
When you compare this code with Lambda Expressions as Function Parameter you see that it is as compact while
allowing us to specify Data Type of Return Value
use normal return
use Syntax that is similar to Named Function Declarations (unlike Lambda that uses completely different Syntax)
Lambda Expressions as Return Value
return { name: String, age: Int -> "$name is $age years old and $weight kg." }
Function as Return Value
//===========================================================================================================
// FUNCTION: getFunction
//===========================================================================================================
fun getFunction (weight: Int) : (name: String, age: Int) -> String {
//DECLARE VARIABLE TO HOLD FUNCTION DATA TYPE.
var anonymousFunction : (name: String, age: Int) -> String
//DECLARE ANONYMOUS FUNCTION.
anonymousFunction = fun (name: String, age: Int) : String {
return "$name is $age years old and $weight kg."
}
//RETURN ANONYMOUS FUNCTION.
return anonymousFunction
//USE ANONYMOUS FUNCTION DECLARATION DIRECTLY AS RETURN VALUE.
return fun (name: String, age: Int) : String { return "$name is $age years old and $weight kg." }
}
//===========================================================================================================
// FUNCTION: main
//===========================================================================================================
fun main() {
//DECLARE VARIABLE TO HOLD FUNCTION DATA TYPE.
var returnedFunction : (name: String, age: Int) -> String
//DECLARE ANONYMOUS FUNCTION USING LAMBDA EXPRESSION.
returnedFunction = getFunction(150) //Returns Closure with partialy initialized Function
//CALL RETURNED FUNCTION.
var result = returnedFunction("John", 20) //Call Function with missing Parameters
//DISPLAY RESULT.
print(result)
}