当前位置:网站首页>Kotlin -- function
Kotlin -- function
2022-07-28 04:07:00 【Violet and begonia】
One 、 Function header

- Used fun Keyword to define the function ;
- The name of the function , The hump nomenclature is used ( In most cases );
- Function parameter , In order to (name: String) In this form , This means that the parameter type is String type ;
- return type , Immediately after the parameter ;
- Finally, the function body in curly braces , It represents the logic of the whole function .
Two 、 Function parameter
Default value parameter
If you do not intend to pass in parameters , You can set default values for parameters in advancefun doSomething(a: Int=1) { }Named value parameter
fun doSomething(a: Int=1) { } doSomething(a = 2)
3、 ... and 、Unit Function of
If a function does not return any useful values , Its return type is Unit.Unit It's a type that has only one value ——Unit The type of . This value does not need to be explicitly returned . Be similar to void, however Unit Function solves the application in generics .
fun printHello(name: String?): Unit {
if (name != null)
println("Hello $name")
else
println("Hi there!")
// `return Unit` perhaps `return` It's optional
}
Four 、TODO function
TODO The function throws an exception , The return value is Nothing type
public inline fun TODO(reason: String): Nothing = throw NotImplementedError("An operation is not implemented: $reason")
for example
fun sum(a: Int, b: Int):Int {
if (a == null || b == null) {
TODO(" abnormal ")
}
return a + b
}
5、 ... and 、 Anonymous functions
Functions that are defined without names are called anonymous functions
Anonymous functions are usually passed to or returned from other functions as a whole , Specify special rules
val s = "abscsabsa" val count = s.count({ letter -> letter == 'a' })
Anonymous function types and implicit returns

- () -> return type
- Unlike named functions , With a few exceptions , Anonymous functions don't need return Keyword returns data , Anonymous functions implicitly or automatically return the result of the last line of the function body
Anonymous function parameters
Like named functions , Anonymous functions can take no arguments , You can also take one or more parameters of any type
The type of the parameter is defined in the type of the anonymous function
The parameter name is placed in the function definition
val blessingFunction: (String,Int) -> String = { name,age -> }it keyword : When defining an anonymous function with only one parameter , have access to it Keyword indicates parameter name
val blessingFunction: (String) -> String = { "hello $it" }
Type inference
- When defining a variable , If you have assigned an anonymous function to it as a variable , You don't need to show the specified variable type

- Type inference also supports anonymous functions with parameters , But anonymous functions Parameter name and parameter type There has to be

When defining parameters, the function of the function
fun showOnBoard(goodsName: String, showDiscount: (String,Int) -> String) {
}
In a nutshell : If a function lambda Parameters last ,, Or the only parameter , Then cover it lambda A pair of parentheses for parameters can be omitted
val blessingFunction = {
"hello"
}
6、 ... and 、 Function inlining
When inlining Kotlin It's an optimization mechanism , With inlining ,JVM No need to use lambda Object instance , Thus, variable memory allocation is avoided . Where need to be lambda, The compiler will copy and paste the function body to where . Otherwise ,JVM Will be the same for all lambda Allocate memory for the variables you deal with , This creates memory overhead , Performance issues .
7、 ... and 、 Function reference
Function references can convert a named function into a value parameter , Use lambda Where the expression is , You can use function references 
8、 ... and 、 Function type as return type

Nine 、 Closure
- stay Kotlin in , Anonymous functions can modify and reference variables defined outside their scope , And one function references the variables of another function ,Kotlin Medium lambda It's a closure

边栏推荐
- 常用的接口测试工具
- Skillfully use stack backtracking to help you quickly locate problems
- cookie与Session
- Shell rental reptile
- [untitled]
- Kingbasees Security Guide for Jincang database -- 6.1 introduction to strong authentication
- Advanced Mathematics (Seventh Edition) Tongji University exercises 3-6 personal solutions
- Dynamic planning - 62. Different paths
- un7.27:redis数据库常用命令。
- [image classification] 2021 MLP mixer nips
猜你喜欢
随机推荐
Dynamic planning - 1049. Weight of the last stone II
7/27(板子)染色法判定二分图+求组合数(递推公式)
Basic knowledge of day08 redis
Analysis of static broadcast transmission process
Appnium--APP自动化测试工具
Program life | test engineers only know a little? Seven shortcuts teach you to learn new technology quickly
2022.7.13-----leetcode.735
Greed 122. The best time to buy and sell stocks II
[untitled]
ServletContext、request、response
ftp服务器、nfs服务器的搭建和使用
[untitled]
H265/HEVC名词解释-- CTU,CTB,CU,CB,TU,PU,TB,PB,LCU,Slice,Tile,Chroma,Luma,I帧,B帧,P帧
Chinese Remainder Theorem of X problem
测试用例管理工具
7/27 (board) dyeing method to determine bipartite graph + find combination number (recursive formula)
【无标题】
过滤器、拦截器、监听器
LeetCode 0140. 单词拆分 II
Kingbasees Security Guide for Jincang database -- 5.2. data integrity protection




![[untitled]](/img/e9/4b00244b67af5ddaa3f35baa1ac968.png)



