User-defined functions
Define your own functions in two ways: in Scripting with :=, or from JavaScript with nerdamer.setFunction(...). Either way, you then use them like built-in functions.
In Scripting
Write the name and parameters on the left of :=, and the body on the right.
import nerdamer from 'nerdamer';
nerdamer('f(x):=x^2');
nerdamer('f(3)').text();
// 9
A function can take several parameters:
nerdamer('g(x,y):=x^2+y');
nerdamer('g(3,4)').text();
// 13
nerdamer(...) calls. Defining the same name again replaces it.Functions work anywhere a built-in one does: with symbolic arguments, and inside diff or integrate.
nerdamer('f(x):=x^2');
nerdamer('f(a+1)').text(); // (1+a)^2
nerdamer('diff(f(x),x)').text(); // 2*x
nerdamer('integrate(f(x),x)').text(); // (1/3)*x^3
From JavaScript
Use nerdamer.setFunction(name, parameters, body) when your code creates the definition. The parameters and body are strings in Notation.
import nerdamer from 'nerdamer';
nerdamer.setFunction('f', ['x', 'y'], 'x^2+y');
nerdamer('f(4,7)').text();
// 23
See nerdamer.setFunction() for its options.
Removing functions
nerdamer.clearFunctions() removes your functions and leaves the built-in ones. Pass a list of names to remove only those.
nerdamer.setFunction('f', ['x'], 'x^2');
nerdamer.setFunction('g', ['x'], 'x+1');
nerdamer.clearFunctions(['f']); // Keep g
nerdamer.clearFunctions(); // remove the rest
After a function is removed, its name is an ordinary variable again: f(2) then means 2*f.
Which to use
Use f(x):=... when the definition is part of the formula or script. Use nerdamer.setFunction(...) when your code builds it. The result is the same.
Redefining a function
nerdamer('f(x):=x^2');
nerdamer('f(3)').text();
// 9
nerdamer('f(x):=x+1');
nerdamer('f(3)').text();
// 4
For if, loops, let and block, see Scripting.
