Roughly cover the materials in Chapters 4 and 5.
Why Functions
- Consider the following code:
- Can we simplify the main function for readability?
- Reuse the
when
expression
- Functions are reusable protion of code
- With proper use, functions make your code more readable.
Refactor to Function
- Select the part of code
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Right click -> Refactor -> Function
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Give proper names to function and parameters
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- IntelliJ detects duplicates
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Header, Body, Scope
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
private
: Visibility modifier
private
means "can only be seen in the same file".
public
means "can be seen everywhere" and is the default. When you ignore the visibility modifier, it is equivalent to use public
.
- We shall discuss other visibility modifiers later.
fun
: function declaration keyword
- Like
var
and val
for variables and values
scoreToGPA
: function name
(score: Int)
: function parameters
- Can be none:
()
- Can be multiple:
(arg1: Type1, arg2: Type 2, arg3: Type3)
: Double
: return data type
- The type of the output data of the function
Body
- Contain the reusable code.
- Use
return
statement to end the function and pass the result to "caller".
- Special case: if the return type is omitted in header or the return type is
Unit
return
does not need a value to pass to caller.
- When the code runs to the end of the body, the function automatically returns.
Scope
- The function:
- Defined by visibility modifier
- The variables and values in function body
- Cannot be seen by other function (function bodies)
- File-level variables and values
- Defined outside of function
- Can be seen by all function bodies in the file
Call a Function
functionName(arg1,...,argN)
- Examples:
scoreToGPA(56)
- After the execution of
scoreToGPA
, the return value will "replace" scoreToGPA(56)
with 1.0
in the statement.
print("Please input an integer: ")
- Often used as a statement
- Member function:
- Function associated with certain type of data
- Example:
Scanner.next
, Scanner.nextInt
, Scanner.nextDouble
Practice: Write Functions
- Modify the function
isLeapYear
- Modify the function
greet
to print greeing messages with name.
- Modify the function
sumUp
to compute the sum of elements in an IntProgression
- Modify the function to compute the average of two
Double
values.
- Complete the function to compute the median of three
Int
values.
Default Argument and Named Argument
Single-Expression Functions
If the function body is simply
then we can replace it with
is equivalent to
is equivalent to
is equivalent to
Reserved Words and Backticks
- Reserved words in Kotlin
val
, var
, fun
, if
, else
, when
, while
, for
, …
- They are invalid identifiers
- Use backticks
`
to escape.
- Example:
System.`in`
Anonymous Functions
-
Function defined with a name: named function
-
Function defined without a name: anonymous function
-
Define anonymous functions like data
- With proper function type
- With implicit return
- With
it
or proper arguments
-
Examples
Function Type
- Like data, functions have types.
fun funName(arg1: Type1,...,argN: TypeN): RetType
funName
has type (Type1,...,TypeN) -> RetType
- Unit Function:
(Type1, ..., TypeN) -> Unit
- Example:
main
has type () -> Unit
scoreToGPA
has type (Int) -> Double
Implicit Returns
- The last expression in the function body of anonymous function is the return value
- You may not use
return
in anonymous function.
Argument Name
- When we define an anonymous function, we do not define the names of the arguments with their types.
- Names are defined in the function body.
is equivalent to
it
Keyword
When the function has exactly one argument, then we can use it
to simplify the code.
Type Inference Support
- Specify the types of the arguments, then Kotlin can derive the type of the anonymous function.
is equivalent to
Function Reference
-
Since we have function types in Kotlin, we can use function types as arguments and return types.
-
String.count
is a function that accepts a function as an argument. See the document.
-
Function Reference: use ::
operator to obtain the "reference" of a named function.
- Short hand:
- If the only argument is a function and we are going to pass an anonymous function, then we can ignore the brackets
(
and )
to simplify the notation.
- If the last argument is a function and we are going to pass an anonymous function, then we can omit the last argument and append the anonymous function.