Skip to content

Multiline Functions

CalcpadCE lets a function definition span multiple lines using _ continuations and the if / $while constructs, so recursive, conditional and iterative routines fit into the same f(x; y; …) = … syntax as plain one-liners.

The four worksheets in this group show that pattern at work. Cubic Roots Function packages the closed-form solution of \(x^3 + a x^2 + b x + c = 0\) as a single multiline call returning all three roots; Quadratic Roots Function does the same for \(a x^2 + b x + c = 0\) with discriminant-based branching. Fibonacci Numbers defines \(fib(n)\) recursively to illustrate self-referential calls, and Greatest Common Divisor - Euclid Algorithm builds an iterative \(gcd(a, b)\) around a $while block.

Together the four pages serve as templates for any user-defined function that needs more than a single expression.

Cubic Roots Function

Multiline cubic-roots function returning all three roots of \(x^3 + a x^2 + b x + c = 0\) via the Cardano formula, with discriminant-based real/complex branching.

Code:
"Cubic Equation Roots Function
'For solving equations of type:'f(x) = x^3 + a*x^2 + b*x + c'= 0

#rad
cubicRoots_1(a; Q; R) = $block{
  A = -sign(R)*(abs(R) + sqr(R^2 - Q^3))^(1/3);
  x_1 = if(A  0; -a/3; A + Q/A - a/3);
  x = hp([x_1; x_1; x_1]);
}
cubicRoots_3(a; Q; R) = $block{
  θ = acos(R/sqr(Q^3));
  x_1 = -2*sqr(Q)*cos(θ/3) - a/3;
  x_2 = -2*sqr(Q)*cos((θ - 2*π)/3) - a/3;
  x_3 = -2*sqr(Q)*cos((θ + 2*π)/3) - a/3;
  x = hp([x_1; x_2; x_3]);
}
cubicRoots(a; b; c) = $block{
  Q = (a^2 - 3*b)/9;
  R = (2*a^3 - 9*a*b + 27*c)/54;
  x = if(R^2 < Q^3; cubicRoots_3(a; Q; R); cubicRoots_1(a; Q; R));
}
x = cubicRoots(-6; 11; -6)
#hide
a = -6', 'b = 11', 'c = -6
d = max(abs(x.3 - x.1)/4; 0.5)
x_min = min(x.1; x.3) - d','x_max = max(x.1; x.3) + d
PlotWidth = 200', 'PlotHeight = 150
#show
'Plot
$Plot{f(ξ) & x.1|0 & x.2|0 & x.3|0 @ ξ = x_min : x_max}
Rendered Output:
Cubic Equation Roots Function

For solving equations of type: f ( x )  = x3 + a · x2 + b · x + c = 0

 

cubicRoots1 ( a; Q; R )  = 🞀A = -sign ( R )  ·  ( |R| +    R2Q3 ) 13
x1 = {if A ≡ 0: -a3
else: A + QAa3

x = hp ( [x1; x1; x1] ) 

cubicRoots3 ( a; Q; R )  = 🞀θ = acos(R   Q3)
x1 = -2 ·    Q · cos(θ3)a3
x2 = -2 ·    Q · cos(θ − 2 · π3)a3
x3 = -2 ·    Q · cos(θ + 2 · π3)a3
x = hp ( [x1; x2; x3] ) 

cubicRoots ( a; b; c )  = 🞀Q = a2 − 3 · b9
R = 2 · a3 − 9 · a · b + 27 · c54
x = {if R2 < Q3: cubicRoots3  ( a; Q; R ) 
else: cubicRoots1  ( a; Q; R ) 

x = cubicRoots  ( -6; 11; -6 )  = [1 2 3]

Plot

Plot

Fibonacci Numbers

Recursive multiline definition of the Fibonacci function \(fib(n)\), illustrating self-referential calls and conditional base cases.

Code:
'Function:
fib(n) = if(n  1; 1;
  $block{
    a = 0;
    b = 1;
    $repeat{
       c = a + b;
       a  b;
       b  c;
     @ k = 1 : n - 1} _
  } _
)
'Results:
#for i = 1 : 9
    fib(i)
#loop
Rendered Output:

Function:

fib ( n )  = {if n ≡ 1: 1
else: 🞀a = 0
b = 1
$Repeat 🞀for k = 1...n − 1
c = a + b
ab
bc

Results:

fib  ( i )  = fib  ( 1 )  = 1

fib  ( i )  = fib  ( 2 )  = 1

fib  ( i )  = fib  ( 3 )  = 2

fib  ( i )  = fib  ( 4 )  = 3

fib  ( i )  = fib  ( 5 )  = 5

fib  ( i )  = fib  ( 6 )  = 8

fib  ( i )  = fib  ( 7 )  = 13

fib  ( i )  = fib  ( 8 )  = 21

fib  ( i )  = fib  ( 9 )  = 34

Greatest Common Divisor - Euclid Algorithm

Iterative greatest common divisor built around a \(while\) block, implementing the classical Euclid algorithm.

Code:
'Custom function:
mygcd(a; b) = $while{
  b > 0;
  t = b;
  b = ab;
  a = t}
'Example:
mygcd(1071; 462)
'Internal gcd function:
gcd(1071; 462)
Rendered Output:

Custom function:

mygcd ( a; b )  = 🞀while b > 0
t = b
b = a mod b
a = t

Example:

mygcd  ( 1071; 462 )  = 21

Internal gcd function:

gcd ( 1071; 462 )  = 21

Quadratic Roots Function

Multiline quadratic-roots function returning both roots of \(a x^2 + b x + c = 0\) via the discriminant, with real/complex branching.

Code:
"Quadratic Equation Roots function
'Multiline code block example:
quadRoots(a; b; c) = _
$block{
    D = b^2 - 4*a*c;
    x_1 = (-b - sqrt(D))/(2*a);
    x_2 = (-b + sqrt(D))/(2*a);
    [x_1; x_2];
}
quadRoots(2; 3; -5)
'Inline code block example:
quadRoots(a; b; c) = $inline{D = b^2 - 4*a*c; x_1 = (-b - sqrt(D))/(2*a); x_2 = (-b + sqrt(D))/(2*a); [x_1; x_2]}
quadRoots(2; 3; -5)
Rendered Output:
Quadratic Equation Roots function

Multiline code block example:

quadRoots ( a; b; c )  = 🞀D = b2 − 4 · a · c
x1 = -b −    D2 · a
x2 = -b +    D2 · a
[x1; x2]

quadRoots  ( 2; 3; -5 )  = [-2.5 1]

Inline code block example:

quadRoots ( a; b; c )  = 🞀D = b2 − 4 · a · c; x1 = -b −    D2 · a; x2 = -b +    D2 · a; [x1; x2]

quadRoots  ( 2; 3; -5 )  = [-2.5 1]

Spotted an error? Edit these examples.