For functions, languages today are increasingly trying to rely on variables.
Indeed, for a function, it should have many variable characteristics.
existKotlin
Defining functions is a very simple thing. I think programming is sometimes highly complicated by some languages, and naming is very simple, and many concepts are introduced. In fact, these are of no use for project development, but just some programming skills, and the readability and stability of this project are of no use.
Function declaration
The keyword of the function isfun
In fact, when some newer languages take these keywords, I suggest that there is a consistent specification. After looking at multiple languages, you will find that some language naming functions are used.fun
Some usefunc
, in fact, these keywords have no impact on the implementation of functions. Different languages take different keywords, which only adds more burden to programming users.
fun myAdd(a:Int,b:Int):Int{ return a+b }
In this way we add a function name:myAdd
, two variables, a and b, return value isInt
. WhyKotlin
Is the return value after?
actuallyKotlin
There is still some reason for this design. It makes the function look the same as the variable, and the type of the function is its return type. This way, it is memorized with the variable, and it is very universal.
Function default parameters
Some languages do not provide default parameters.Kotlin
It is provided and functions with default parameters are quite comfortable to use. For example, when you are reading other people's APIs, you don't need to understand all the parameters, just default.
fun isA(v:Char,ignorecase:Boolean=true):Boolean{ var c:Char = '1' if (ignorecase) c = () return c == 'a' }
If you have multiple default parameters, you can assign values to the default variables by specifying names instead of considering too much about parameter order.
var ba:Boolean = isA('A', ignorecase = false)
Variable parameters
Kotlin
Support for parameters still gives a lot of space. Variable parameters are usually rare, but there is one you must have used, that is, when printing logs.
fun myLog(vararg args: String) { println(args::class) println(()) for (i in ) { println(args[i]) } }
We can call this:
myLog("1","2","3")
Kotlin
Variable parameters can be understood as variables are arrays or linked lists. This is the best way to understand them, and it should be like this.
summary
Functions cannot be used in programming. They are the basis for reusing our program. In this section, we also discuss some language design issues. A good language needs to be simple enough and have a certain degree of universality. This is truly good for programmers.
This is the end of this article about the usage of Kotlin fun function. For more related content of Kotlin fun function, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!