HP Prime for All

English  Русский 

Author: Edward Shore (original article)

This session will show how routines work in HPPL.

Generally, subroutines have be declared before the main program. Declaration is important. The details of the subroutines are after the main program.

Definitely take a look at the example programs to get a better understanding.

General Syntax:

sub(); //declare subroutines

EXPORT main()
BEGIN commands go here, including sub()
END;

sub()
BEGIN commands go here
END;

SUBEXAM

This is just a demonstration of how sub routines work.
This program calculates one of two values:

Where:
A = 2(x-y)/Φ + xy
B = Φ^2
and
Φ = 2e^(x+y) - e^(x-y) - e^(y-x)

We will use Φ as the subroutine.

SUB1();

EXPORT SUBEXAM(X, Y)
BEGIN LOCAL A, B;
A := (2 * (Y-X)) / SUB1(X, Y) + X*Y;
B := (SUB1(X, Y))^2;
IF A > B THEN RETURN A;
ELSE
RETURN B;
END;
END;

SUB1(X, Y)
BEGIN RETURN 2*e^(X+Y) - e^(X-Y) - e^(Y-X);
END;

Examples:
SUBEXAM(-4, 1) returns 21998.918189
SUBEXAM(2,3) returns 86283.2797974
SUBEXAM(-5,-6) returns 30.648061288
SUBEXAM(2,-3) returns 21810.6046664

Days Between Dates

DDAYS Using Subroutines for HP Prime: Best for 1901 to 2099

Source: HP 12C Manual - Hewlett Packard

// Declare Subroutines
SUB1();
SUB2();
SUB3();

// Main program
EXPORT DDAYS(m1, d1, y1, m2, d2, y2)
BEGIN // ΔDYS HP 12C
LOCAL x1, x2, z1, z2;
x1 := SUB1(m1); x2 := SUB1(m2);
z1 := SUB2(m1, y1); z2 := SUB2(m2, y2);
RETURN SUB3(y2, m2, d2, z2, x2)-
SUB3(y1, m1, d1, z1, x1);
END;

SUB1(X)
BEGIN IF X ≤ 2 THEN RETURN 0;
ELSE
RETURN IP(.4*X+2.3);
END;
END;

SUB2(X, Y)
BEGIN IF X ≤ 2 THEN RETURN Y-1;
ELSE
RETURN Y;
END;
END;

SUB3(Y, M, D, Z, X)
BEGIN RETURN 365*Y+31 * (M-1) + D+IP(Z/4) - X;
END;

Examples:
Days Between Dates:

So that is how subroutines work. Please give comments, ask questions, and always thanks to my supporters and readers. Cheers!

Read Next Part >

Comments