SlideShare a Scribd company logo
1 of 36
Download to read offline
Functions, Anonymous Functions
and the Function Type
Kotlin Study Group
John
Hi, I am John
- Android Developer
- Work on Rakuten Viki
- Use Kotlin for 2 years
- Linkedin:
https://www.linkedin.com/in/chang-john-0a95237a/
Outline
- What’s a Function?
- Call a Function
- Anatomy of a Function
- Function Header
- Visibility Modifier
- Function Name declaration
- Function Return Type
- Function Body
Outline
- Named Functions v.s Anonymous Functions
- Function Type
- Implicit returns
- it keyword
- Function Inference Support
- High-Order Function
- Lambda/Function Reference/Anonymous Function
- Function Inlining
- Closure Behavior
- Function Support: Kotlin v.s Java
- Q&A
Q&A
- How to achieve backticks symbol naming?
- How Top level function in Kotlin is achieved on JVM?
- How default arguments in Kotlin is achieved on JVM?
- How naming function arguments in Kotlin is achieved on JVM?
- How to achieve Nothing type and TODO() function?
What’s a Function?
Call a Function
Anatomy of a Function
- Visibility modifier
- Use private/internal/public
- Without specify any visibility modifier, public is used by default
- Visibility Modifier definition
- Function Name
- Name by camel case with no underscores for convention.
- Surround using backticks symbol, like fun `This is my function`() { .. .}
- Function Parameters
- The Format is variableName: Type in order. No need to add val/var in advance.
- Default arguments, like fun log(tag: String = “”, msg: String) { … }
- Naming Function Arguments, like log(tag = “Main”, msg = “print function”)
- Function Parameters
- Function Overloading
- Return Type
- Return type can any type, primitive or class
- If no specify return type, default type is Unit
- Nothing type lets the compiler know that a function is guaranteed to NEVER successfully
complete, like TODO()
- Function Body
- Function Scope
- Single-Expression Function
Named Function v.s Anonymous Function
Function Type
Function Arguments
Function Arguments - it keyword
Type Inference Support
Define a Function That Accepts a Function
If we define a function that accepts a function, we can call it
high-order function
If we define a function that accepts a function, we can call it
high-order function
Reference from High-Order Function and Lambda
How to pass a instance of function type as arguments?
Use Lambda
If function type is last parameter, it even pass Lambda with
the shorthand syntax
How to pass a instance of function type as arguments?
Use Function Reference
How to pass a instance of function type as arguments?
Use Anonymous Function
Function Inlining
Function Inlining - without inline
Function Inlining - use inline
Closure Behavior
Function Support: Kotlin v.s Java
- Java version
Function Support: Kotlin v.s Java
- Kotlin version
Q&A
How to achieve backticks symbol naming on JVM?
Kotlin
Java
How Top level function in Kotlin is achieved on JVM?
Kotlin
Java
How default arguments in Kotlin is achieved on JVM?
Kotlin
Java
How naming function arguments in Kotlin is achieved on
JVM?
Kotlin
Java
How to achieve Nothing type and TODO() function?
TODO
Nothing

More Related Content

What's hot

Why should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming ParadigmWhy should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming Paradigm
Tech Triveni
 

What's hot (20)

Xtend - better java with -less- noise
Xtend - better java with -less- noiseXtend - better java with -less- noise
Xtend - better java with -less- noise
 
Concurrent Applications with F# Agents
Concurrent Applications with F# AgentsConcurrent Applications with F# Agents
Concurrent Applications with F# Agents
 
Why should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming ParadigmWhy should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming Paradigm
 
[2021] kotlin built-in higher-order functions
[2021] kotlin built-in higher-order functions[2021] kotlin built-in higher-order functions
[2021] kotlin built-in higher-order functions
 
Virtual function and abstract class
Virtual function and abstract classVirtual function and abstract class
Virtual function and abstract class
 
Kotlin L → ∞
Kotlin L → ∞Kotlin L → ∞
Kotlin L → ∞
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Swift, a quick overview
Swift, a quick overviewSwift, a quick overview
Swift, a quick overview
 
JavaScript Introductin to Functions
JavaScript Introductin to FunctionsJavaScript Introductin to Functions
JavaScript Introductin to Functions
 
Actors and Threads
Actors and ThreadsActors and Threads
Actors and Threads
 
Functional programming for the Advanced Beginner
Functional programming for the Advanced BeginnerFunctional programming for the Advanced Beginner
Functional programming for the Advanced Beginner
 
From Perl To Elixir
From Perl To ElixirFrom Perl To Elixir
From Perl To Elixir
 
Variables in Pharo5
Variables in Pharo5Variables in Pharo5
Variables in Pharo5
 
Jasmine
JasmineJasmine
Jasmine
 
Building Apis in Scala with Playframework2
Building Apis in Scala with Playframework2Building Apis in Scala with Playframework2
Building Apis in Scala with Playframework2
 
Building a blog with an Onion Architecture
Building a blog with an Onion ArchitectureBuilding a blog with an Onion Architecture
Building a blog with an Onion Architecture
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Introduction to Scala language
Introduction to Scala languageIntroduction to Scala language
Introduction to Scala language
 
Js scope
Js scopeJs scope
Js scope
 
MVC Gems
MVC GemsMVC Gems
MVC Gems
 

Similar to Functions, anonymous functions and the function type

Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
Deepak Singh
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
miki304759
 

Similar to Functions, anonymous functions and the function type (20)

FUNCTIONS IN R PROGRAMMING.pptx
FUNCTIONS IN R PROGRAMMING.pptxFUNCTIONS IN R PROGRAMMING.pptx
FUNCTIONS IN R PROGRAMMING.pptx
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
 
Kotlin functions
Kotlin functionsKotlin functions
Kotlin functions
 
FUNCTIONS.pptx
FUNCTIONS.pptxFUNCTIONS.pptx
FUNCTIONS.pptx
 
FUNCTION CPU
FUNCTION CPUFUNCTION CPU
FUNCTION CPU
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
 
polymorphism and virtual function
polymorphism and virtual functionpolymorphism and virtual function
polymorphism and virtual function
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 
04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx04. WORKING WITH FUNCTIONS-2 (1).pptx
04. WORKING WITH FUNCTIONS-2 (1).pptx
 
Introduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTXIntroduction to Kotlin - Android KTX
Introduction to Kotlin - Android KTX
 
Function different types of funtion
Function different types of funtionFunction different types of funtion
Function different types of funtion
 
Php, mysq lpart3
Php, mysq lpart3Php, mysq lpart3
Php, mysq lpart3
 
python 165813219816514981616098813219 (1).pptx
python 165813219816514981616098813219 (1).pptxpython 165813219816514981616098813219 (1).pptx
python 165813219816514981616098813219 (1).pptx
 
Scala functions
Scala functionsScala functions
Scala functions
 
TypeScript 101
TypeScript 101TypeScript 101
TypeScript 101
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
 
Userdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdfUserdefined functions brief explaination.pdf
Userdefined functions brief explaination.pdf
 
Chapter One Function.pptx
Chapter One Function.pptxChapter One Function.pptx
Chapter One Function.pptx
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 

More from Chang John (10)

Whats resources compat_contextcompat_and_appcompat
Whats resources compat_contextcompat_and_appcompatWhats resources compat_contextcompat_and_appcompat
Whats resources compat_contextcompat_and_appcompat
 
Java interoperability
Java interoperabilityJava interoperability
Java interoperability
 
Interface and abstract classes
Interface and abstract classesInterface and abstract classes
Interface and abstract classes
 
Objects
ObjectsObjects
Objects
 
Inheritance
InheritanceInheritance
Inheritance
 
Initialization
InitializationInitialization
Initialization
 
Defining classes
Defining classesDefining classes
Defining classes
 
Strings & numbers
Strings & numbersStrings & numbers
Strings & numbers
 
Variables, constants, types, and conditionals
Variables, constants, types, and conditionalsVariables, constants, types, and conditionals
Variables, constants, types, and conditionals
 
Google Play Instant: Improving User Engagement
Google Play Instant: Improving User EngagementGoogle Play Instant: Improving User Engagement
Google Play Instant: Improving User Engagement
 

Recently uploaded

Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 

Recently uploaded (20)

Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 

Functions, anonymous functions and the function type

  • 1. Functions, Anonymous Functions and the Function Type Kotlin Study Group John
  • 2. Hi, I am John - Android Developer - Work on Rakuten Viki - Use Kotlin for 2 years - Linkedin: https://www.linkedin.com/in/chang-john-0a95237a/
  • 3. Outline - What’s a Function? - Call a Function - Anatomy of a Function - Function Header - Visibility Modifier - Function Name declaration - Function Return Type - Function Body
  • 4. Outline - Named Functions v.s Anonymous Functions - Function Type - Implicit returns - it keyword - Function Inference Support - High-Order Function - Lambda/Function Reference/Anonymous Function - Function Inlining - Closure Behavior - Function Support: Kotlin v.s Java - Q&A
  • 5. Q&A - How to achieve backticks symbol naming? - How Top level function in Kotlin is achieved on JVM? - How default arguments in Kotlin is achieved on JVM? - How naming function arguments in Kotlin is achieved on JVM? - How to achieve Nothing type and TODO() function?
  • 8. Anatomy of a Function
  • 9. - Visibility modifier - Use private/internal/public - Without specify any visibility modifier, public is used by default - Visibility Modifier definition - Function Name - Name by camel case with no underscores for convention. - Surround using backticks symbol, like fun `This is my function`() { .. .} - Function Parameters - The Format is variableName: Type in order. No need to add val/var in advance. - Default arguments, like fun log(tag: String = “”, msg: String) { … } - Naming Function Arguments, like log(tag = “Main”, msg = “print function”)
  • 10. - Function Parameters - Function Overloading
  • 11. - Return Type - Return type can any type, primitive or class - If no specify return type, default type is Unit - Nothing type lets the compiler know that a function is guaranteed to NEVER successfully complete, like TODO()
  • 12. - Function Body - Function Scope - Single-Expression Function
  • 13. Named Function v.s Anonymous Function
  • 16. Function Arguments - it keyword
  • 18. Define a Function That Accepts a Function
  • 19. If we define a function that accepts a function, we can call it high-order function
  • 20. If we define a function that accepts a function, we can call it high-order function Reference from High-Order Function and Lambda
  • 21. How to pass a instance of function type as arguments? Use Lambda
  • 22. If function type is last parameter, it even pass Lambda with the shorthand syntax
  • 23. How to pass a instance of function type as arguments? Use Function Reference
  • 24. How to pass a instance of function type as arguments? Use Anonymous Function
  • 26. Function Inlining - without inline
  • 27. Function Inlining - use inline
  • 29. Function Support: Kotlin v.s Java - Java version
  • 30. Function Support: Kotlin v.s Java - Kotlin version
  • 31. Q&A
  • 32. How to achieve backticks symbol naming on JVM? Kotlin Java
  • 33. How Top level function in Kotlin is achieved on JVM? Kotlin Java
  • 34. How default arguments in Kotlin is achieved on JVM? Kotlin Java
  • 35. How naming function arguments in Kotlin is achieved on JVM? Kotlin Java
  • 36. How to achieve Nothing type and TODO() function? TODO Nothing