Skip to main content

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

functions.mux
Loading...
  • Keyword func declares a function
  • Parameter list with explicit types (no type inference with auto allowed)
  • returns clause specifies return type (explicit, no inference)
  • Body enclosed in {...} with no semicolons
  • Local variables within functions can use auto inference

Default Parameters

default_params.mux
Loading...

Return Types

All functions must explicitly declare their return type:

return_types.mux
Loading...

Unused Parameters

Use _ for parameters you don't need:

unused_params.mux
Loading...

This is useful when implementing interfaces or callbacks that require specific signatures.

Lambdas and Closures

Mux supports anonymous functions (lambdas) using block syntax:

lambdas.mux
Loading...

Using Lambdas with Collections

lambda_collections.mux
Loading...

Generic Functions

Functions can be generic (see generics and interfaces) over type parameters:

generic_functions.mux
Loading...

Built-in Functions

Mux provides essential built-in functions available without imports:

Output

print(string message) -> void - Outputs to stdout with newline:

print_example.mux
Loading...

Input

read_line() -> string - Reads a line from stdin:

read_line_example.mux
Loading...

Utility

range(int start, int end) -> list<int> - Creates a list of integers, inclusive of start and exclusive of end:

range_example.mux
Loading...

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

function_calls.mux
Loading...

Calling main() Explicitly

Mux allows explicit calls to main() from user code.

main_call.mux
Loading...

The startup entrypoint still calls main() once automatically. If user code also calls main(), it will execute multiple times.

Best Practices

  1. Explicit types for parameters and return values - Makes function signatures clear
  2. Use auto for local variables - Reduces verbosity when types are obvious
  3. Return result<T, E> for fallible operations - Better than panicking
  4. Return optional<T> for nullable values - Explicit absence handling
  5. Use _ for truly unused parameters - But prefer descriptive names when helpful
  6. Keep functions small and focused - Single responsibility

See Also