English
Summary
Conclusions
A function has a contract that includes parameter and return types, units, valid bounds, and how it fails. Unit denotes an action without a useful result, Nothing denotes a path without a normal return, and require explicitly rejects an invalid argument. Default parameters and named arguments make a call clear, but their values and names become part of the API. vararg accepts a variable number of arguments, while recursion requires a base case and a step that moves toward it; tailrec converts a tail call into a loop but does not fix algorithm errors. Extension functions are called like methods, but they are resolved statically and do not change the receiver class. A string is immutable: operations on it return a new string, and String.length on the JVM counts UTF-16 code units. Templates, slices, formatting with an explicit locale, and StringBuilder help produce predictable text. Regex distinguishes checking the entire string with matches from searching for a fragment with find, and syntactically valid text still needs semantic validation.
Self-check questions
- How does returning a value differ from printing?
- When is an expression body convenient?
- What do Unit and Nothing mean?
- What data is a function's hidden input?
- When is a default argument evaluated?
- Why is the vararg spread operator needed?
- Why is the usual factorial implementation not tail-recursive?
- What restrictions does an infix function have?
- Why does an extension not add a field to an object?
- How does a wordCount property differ from a stored field?
- What units does String.length count on the JVM?
- How does matches differ from find?