Language reference

User-defined functions

Module-level positional functions with local, pass-by-value arguments.

01

Declare and call

A function has zero to 32 named outputs. Single-output syntax remains unchanged; bracketed lists declare multiple outputs. Definitions in a script are registered before execution, so a call may precede its declaration.

Sun
answer = square(5);

function result = square(value)
    result = value * value;
end
02

Scope and dispatch

  • Calls receive only positional parameters as local variables.
  • Matrix arguments are passed by value.
  • A user function can shadow a built-in.
  • A same-named matrix variable takes precedence and is indexed.
  • Recursion is limited to 32 simultaneous calls.
03

Functions in other files

Calls can load a same-named .sun file from the caller directory, project root or configured search paths. Each automatically loaded file must contain exactly one matching function and no script statements. See Function projects for configuration, reload and Studio folder permissions.

04

Function handles / Handles de funcoes

EN: @name creates a named function handle; @(args) expression creates an anonymous function capturing existing free variables by value. Call a handle through its variable, including multiple-output assignment for named handles. nargout() and isargout(k) inspect the active call, not a function's declared output count. See Multiple outputs for PT/EN contracts.

PT: @nome cria um handle nomeado; @(args) expressao cria uma funcao anonima que captura variaveis livres existentes por valor. Chame o handle pela variavel, inclusive com atribuicao multipla para handles nomeados. nargout() e isargout(k) consultam a chamada ativa, nao a quantidade declarada de saidas.

Sun
offset = 10;
shift = @(value) value + offset;
offset = 20;
disp(shift(2) == 12);
handle = @square;
disp(handle(3) == 9);

function result = square(value)
    result = value * value;
end
05

Current limits

Nested function declarations, optional arguments, nargin, variable argument/output lists and nargout(f) are not implemented. Native compilation rejects user functions and handles with interpreter guidance. User-function calls, handles, nargout and isargout remain unsupported inside parfor workers.

Sun is proprietary software under experimental development. Documentation scope: Sun 0.6 development snapshot.