| Start | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Function References We can reference a function without actually calling it by prefixing the function's name with Functions without a receiver will be converted to Functions with a receiver (be it an extension function or a member function) has a different syntax. You have to add the type name of the receiver before the double colon: However, when a function's receiver is an object, the receiver is omitted from parameter list, because these is and only is one instance of such type. Since kotlin 1.1, function reference can also be bounded to a variable, which is then called a bounded function reference. Note this example is given only to show how bounded function reference works. It's bad practice in all other senses. There is a special case, though. An extension function declared as a member can't be referenced. |
||||||||||||||||
| Basic Functions Functions are declared using the A shorthand version of the same: And the type can be omitted since it can be inferred: |
||||||||||||||||
| Inline Functions Functions can be declared inline using the One difference from C macros is that inline functions can't access the scope from which they're called: |
||||||||||||||||
| Lambda Functions Lambda functions are anonymous functions which are usually created during a function call to act as a function parameter. They are declared by surrounding expressions with {braces} - if arguments are needed, these are put before an arrow The last statement inside a lambda function is automatically the return value. The type's are optional, if you put the lambda on a place where the compiler can infer the types. Multiple arguments: If the lambda function only needs one argument, then the argument list can be omitted and the single argument be referred to using If the only argument to a function is a lambda function, then parentheses can be completely omitted from the function call. |
||||||||||||||||
| Functions Taking Other Functions As seen in "Lambda Functions", functions can take other functions as a parameter. The "function type" which you'll need to declare functions which take other functions is as follows: For example, you could use the vaguest type, |
||||||||||||||||
| Operator functions Kotlin allows us to provide implementations for a predefined set of operators with fixed symbolic representation (like More operator functions can be found in here (opens new window) |
||||||||||||||||
| Shorthand Functions If a function contains just one expression, we can omit the brace brackets and use an equals instead, like a variable assignment. The result of the expression is returned automatically. Syntax
Parameters
|
Read Article
https://develop.horje.com/learn/1434/reference