Functions
Mux functions use the func keyword with explicit parameter types and return type declarations in the form of:
func <function_name>(<parameter_types> <parameters>) returns <return_type> { ... }
Basic Function Definition
- Keyword
funcdeclares a function - Parameter list with explicit types (no type inference with
autoallowed) returnsclause specifies return type (explicit, no inference)- Body enclosed in
{...}with no semicolons - Local variables within functions can use
autoinference
Default Parameters
Return Types
All functions must explicitly declare their return type:
Unused Parameters
Use _ for parameters you don't need:
This is useful when implementing interfaces or callbacks that require specific signatures.
Lambdas and Closures
Mux supports anonymous functions (lambdas) using block syntax:
Using Lambdas with Collections
Generic Functions
Functions can be generic (see generics and interfaces) over type parameters:
Built-in Functions
Mux provides essential built-in functions available without imports:
Output
print(string message) -> void - Outputs to stdout with newline:
Input
read_line() -> string - Reads a line from stdin:
Utility
range(int start, int end) -> list<int> - Creates a list of integers, inclusive of start and exclusive of end:
Design Note: range() is the primary way to create numeric sequences, as Mux does not support C-style for (int i = 0; i < n; i++) loops.
Function Calling
Calling main() Explicitly
Mux allows explicit calls to main() from user code.
The startup entrypoint still calls main() once automatically. If user code also
calls main(), it will execute multiple times.
Best Practices
- Explicit types for parameters and return values - Makes function signatures clear
- Use
autofor local variables - Reduces verbosity when types are obvious - Return
result<T, E>for fallible operations - Better than panicking - Return
optional<T>for nullable values - Explicit absence handling - Use
_for truly unused parameters - But prefer descriptive names when helpful - Keep functions small and focused - Single responsibility
See Also
- Generics - Generic functions and type constraints
- Error Handling - Using result and optional
- Variables - Type inference with
auto - Control Flow - If/else, loops, and match