**Subroutines**

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.

SUB Routines for HP Prime

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:

If A is positive, then the program evaluates A. If not, the program values B instead. 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

* Remember century years not divisible by 400 are NOT leap years. This program does not take this into account. If any such years are passed, subtract one day for such year manually.

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;

(Thanks to Owitte for pointing out my typo)

Examples:

Days Between Dates:

7/3/1985 to 2/28/1995 is 3,527 days

3/14/1977 to 11/17/2013 is 13,397 days

12/10/2010 to 6/30/2014 is 1,298 days

1/5/2015 to 3/19/2227 returns 77,506 BUT this program treats 2100 and 2200 as leap years, which in reality they are not. Subtract 2 to get the correct answer of 77,504 days.

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

Eddie

This blog is property of Edward Shore. 2013