Kotlin Learning (6) → Function


Looking forward to Mr. Guo Lin ** "first line of code (third edition)" When **, realize that they need to add the necessary Kotlin knowledge. Now the process of writing articles to share their learning the basics of Kotlin, after the fight to get the book will soon get started.
Software using IDEA 2019, article for any errors or lack of place, welcome criticism.
(Refer to "Kotlin from zero to proficient Android development," the Sun Ouyang, Tsinghua University Press, April 2018 first edition, ISBN 978-7-302-49814-8). Thank Mr. Ouyang of outstanding teaching.

Article comes from my Jane books .

1, how to declare a function

The basic function is declared Kotlin mode (e.g. declared type of function return value Int print, no arguments):

fun print():Int {
    println("SSSS.GRIDMAN")
}

Therefore, the basic mode is: Fun function name (parameter list): Return Value {
// operations
}
function name in a small hump nomenclature
If the function does not return type, the return value may be omitted type declaration, or use the following format:

fun print():Unit {
}

This approach is intended to meet the basic mode of function declaration.

2, the parameters for a given function, the default parameters

For example, the need to implement a function, addition with two numbers, and outputs the result.
According to the wording of the function of variable declarations and before the introduction of the Java language, we can get Kotlin code is as follows:
The results obtained in 3 + 4

The default parameter approaches and Java are similar, as follows:
With default parameters
However, this may cause some problems, such as would be confused with no parameters, for example:
Parameter-free function coverage of the original function
So, best to just use the default values for certain parameters, and this the foregoing parameters can not occur in case the default values, and parameters given situation behind value. Examples of the above example, if the statement getAnswer (3) main function of this will be 3 as the parameter x .
Of course, the problem is there is a solution, and that is named parameters.

3, named arguments

In Python, we often see such wording:

wnd = tkinter.Tk(title="tkinter窗体")

Kotlin has a similar wording, it is very clear: we pass the parameter name = value approach, you can let the compiler know which specify the value of a parameter we give and what parameters to use the default value.
For example, the above program to:
Named parameter method
know, main getAnswer function call using default parameters () method and specify the value of y is 4.

4, variable parameters

The variable parameters can always increase the number of parameters. Mr. Young's book in terms of relatively simple and obvious: there are four great inventions of ancient China, but some people think there should be seven, ten invention is not limited to "big four", fortunately Kotlin provide a variable parameter type, you can always increase the number of parameters.
Kotlin variable parameter in processing, it will be processed as an array, we need to remove these values through the circulation.
The general format (for example, declare a variable of type String parameter?) Variable parameters declared
vararg args: String?
Consider the following example:
Examples of variable parameters

Released eight original articles · won praise 0 · Views 76

Guess you like

Origin blog.csdn.net/KamiyamaSatoru/article/details/104062760
Recommended