Functions and built-ins
Learning objective
- Define functions and use built-ins with correct arguments and return values.
Key syntax
func add(a: int, b: int) -> int {
return a + b;
}
print(add(2, 3));
Examples
- Core built-ins:
print, len, input, str, int, float, bool, type_of.
- Utility built-ins:
abs, min, max, pow, round, floor, ceil, random.
- Collection helpers through methods:
- list:
insert, remove, removeAt, extend, first, last, is_empty, copy, clear
- dict:
items, size, merge, put, remove, is_empty, clear
- String helpers through methods:
- formatting:
ltrim, rtrim, pad_left, pad_right, capitalize, title
- conversion/checking:
to_int, to_float, to_bool, count, is_numeric, is_alpha, is_alnum
Common mistakes
- Wrong argument count.
- Return type mismatch.
- Calling non-callable values.
Suggested practice
- Create helper functions for grade average and pass/fail decision.
- builtins-math-date
- diagnostics