Function Declaration declares Named Function (Function that has a Name)
Each Function has Signature which defines
● Data Types of Input Parameters (name:String, age:Int)
● Data Type of Return Value (String)
● Colon ":" inside Signature symbolizes that these Input Parameters are converted into this Output Parameter.
Since Functions can be stored inside Variables or given as Input Parameters into other Functions, Signature is used to say
● what kind of Function can be stored into a Variable
● what kind of Function can be given as Input Parameter into another Function
For instance var name : Int says that this Variable can store Int Data Type.
Similarly var name : (name:String, age:Int) -> (String) says that this Variable can store Function that
● accepts String & Int as Input Parameters
● returns String
Content
● Syntax - Block fun greet (name:String, age:Int) : (String) { ... }
● Syntax - Assignment fun greet (name:String, age:Int) : (String) = Single Line
● Input Parameters fun greet (name:String, age:Int, height: Int = 170) greet("John", age=20)
● Return Value fun greet1(name: String, age: Int) : Unit { ... }
● Function Scope (Top Level, Local, Member, Extension Functions)
● Function as Input Parameter
● Function as Return Value
● Infix Methods (Methods with single Parameter, no default value, no vararg)
Syntax
//NAMED FUNCTION DECLARATIONS.
fun greet (name:String, age:Int = 20) : (String) { return("$name is ${age} years old") }//Block Syntax
fun greet (name:String, age:Int) : (String) = "$name is ${age} years old" //Assignment Syntax
//CALL FUNCTION BY NAME.
greet(name="John", age=50) //Named Parameters
greet("John", age=50) //Indexed before Named Parameters
greet("John", 50) //Indexed Parameters
greet("John") //Default Parameters