>> f = g -> abs(g(0)); >> f(sin) 0 >> f(cos) 1The one-linear definition of a function can be used directly
>> f = g -> abs(g(0)); >> f(x -> sqrt(3 + x^2)) 1.732050808
>> select_func = function code -> f if code == 's' f = sin; elseif code == 'c' f = cos; elseif code == 'a' f = sin + cos else f = x -> sqrt(1 + x^2); end end >> f = select_func('a'); >> f(pi/4) 1.414213562
Also, it is very common and easy to use nested functions -- function definitions inside function definition. For example
create_func = function d -> func ... func = function x -> y y = sqrt(x^2 + 1); end endWhen create_func is called, it creates a function f and return it. Note that inside the definition of func, the value of input argument d is not accessible, so it may seem impossible to create a function based on the input value of create_func. Actually there are two ways to get around this - parameterized functions (See 6.8) and partial substitution (See 6.10).