function

Programming

Define a named function. The function body can contain any SimLab statements. Single output uses "function out = name(...)". Multiple outputs use "[a, b] = name(...)". Functions without output use "function name(...)".

Syntax

function out = name(args) ... end
function [a, b] = name(x, y) ... end
function name(args) ... end

Examples

function y = square(x)
  y = x^2;
end
square(5)
▶ Run
function [s, p] = stats(x)
  s = sum(x);
  p = prod(x);
end
[s, p] = stats([1, 2, 3])
▶ Run

See Also

Try SimLab — Free MATLAB® Alternative

466 functions. Runs in your browser. No install.

Open SimLab

Stay Updated

Get notified about new simulations and tools. We send 1-2 emails per month.