Context Free Art

Function declaration

From Context Free Art

Jump to: navigation, search

In addition to the predefined math functions, a CFDG file can declare new numeric functions. The syntax is very similar to variable declarations: name ( parameters ) = expression. Where name is the function name, parameters are the function parameters, and expression is the numeric expression that is evaluated for the function. The parameters do not have type specifiers like they do for shape parameters because only numeric parameters are allowed.

erf(n) = if(n > 0, 1, -1) * sqrt(1 - exp(-n * n * (4 / 3.1415926535 + 0.147 * n * n) / (1 + 0.147 * n * n)))

Recursive functions are allowed:

// Fibonacci function
fib(n) =
if(n <= 1,
n,
fib(n-1) + fib(n-2)
)
 
// Ackerman function
ack(m, n) =
if(m == 0,
n+1,
if(n == 0,
ack(m-1, 1),
ack(m-1, ack(m, n-1))
)
)

Note that these user functions use the if function, not the if control flow element.

Views
Personal tools
Navigation
Toolbox
Powered by MediaWiki
Attribution-Share Alike 2.5
book coverSee our book:
Community of Variation