anonymous-function

Programming

Create an anonymous (lambda) function. The @ symbol followed by parameter list and expression creates a callable function object. Anonymous functions capture workspace variables at creation time.

Syntax

f = @(args) expression
@(x) x^2
@(x, y) x + y

Examples

f = @(x) x^2;
f(5)
▶ Run
g = @(x, y) sqrt(x^2 + y^2);
g(3, 4)
▶ Run
k = 2;
h = @(x) k*x;  % captures k=2
h(10)
▶ 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.