The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

setting up subroutines that are called many times...
Message #1 Posted by Geoff Quickfall on 17 Oct 2013, 3:46 a.m.

HPPL PRIME:

Hello all, still working on the programming which when I get the code right is fantastic.

I must admit that I have very crude programming abilities at the moment but here goes:

I have the following dilema, I need to create a subroutine, easy in Basic and RPN with label, xeq and gto commands but I am having problems with the HPPL.

In essence I am creating a break schedule program that takes the following variables:

EXPORT OFF,ONA,ONB,IN,OUT,NUM,EQL;

OFF the GMT takeoff (wheels up time) of the aircraft. ONA the GMT landing (wheels down time). ONB the scratch pad for a corrected wheels landing time; actually the landing time if the landing time is less then the OFF time, ie the flight goes through midnight. IN the time returning to the flight deck before landing. OUT the time leaving the flight deck after takeoff. NUM number of breaks. EQL the time if all breaks are equal.

So the break schedule follows:

the input functions prompt for OFF, ON, IN, OUT, NUM.

// the next line tests for takeoff time greater then landing time // and corrects that by adding 24 hours to ONB. This is so total // time for breaks can be calculated. // the reason for ONA versus ONB as ONA must remain untouched for // display purposes at the end of the program.

IF OFF>ONA THEN ONB:=ONA+24 ELSE ONB:=ONA

Now that little routine is used every time a time period is added to a time period making it a perfect subroutine. This is because any additional time period may traverse 24 hours.

My problem is how do I set up a subroutine which is called multiple times. As I am typing I see that it could be a program that is called for each calculation but how is a subroutine within a created APP done.

Still reading and trying but lazy me, any help would be appreciated.

Looking for SYNTAX something like:

ROUTINE X IF OFF>ONA THEN ONB:=ONA + 24 ELSE ONB:=ONA RETURN

Cheers, Geoff

      
Re: setting up subroutines that are called many times...
Message #2 Posted by Nigel J Dowrick on 17 Oct 2013, 6:04 a.m.,
in response to message #1 by Geoff Quickfall

How about declaring a function with OFF and ONA as arguments, returning a value which is assigned to ONB? Something like

ONB := FIXUP(OFF, ONA),

with your "if..." code line making up the body of the function?

Disclaimer: I don't actually have a Prime!

Nigel (UK)

            
Re: setting up subroutines that are called many times...
Message #3 Posted by Han on 17 Oct 2013, 7:27 a.m.,
in response to message #2 by Nigel J Dowrick

Quote:
How about declaring a function with OFF and ONA as arguments, returning a value which is assigned to ONB? Something like

ONB := FIXUP(OFF, ONA),

with your "if..." code line making up the body of the function?

Disclaimer: I don't actually have a Prime!

Nigel (UK)


You would still need to create either a program or subroutine called FIXUP and also ensure that OFF and ONA are properly declared (local vs global).

Let's say that our program is called SCHEDULE. Now, SCHEDULE may actually consist of a bunch of subroutines: SUBR1, SUBR2, SUBR3, etc. Your SCHEDULE source file should look something like:

// subprogram declarations
SUBR1(); // add EXPORT in front if you want to use these programs as standalone
SUBR2(); // or use the subprograms within other programs residing in a different
SUBR3(); // program source file
... more subprogram declarations ...

// global vars. declarations EXPORT OFF; // leave off EXPORT if you want them "hidden" from the user EXPORT ONA; ... more gobal variable declarations

EXPORT SCHEDULE() BEGIN ... source code for schedule here ... END;

SUBR1() // add EXPORT in front to make this visible to users and/or other programs BEGIN ... source code for subroutine 1 here... END;

SUBR2() BEGIN ... source code for subroutine 2 here... END

... and so on...

Hope that helps. In a sense, it is very similar to C. Normally we have a .h file in C where we store all the function declarations (as well as global variable declarations) and use an include blah.h in the actual C source file at the top. The difference is the the HP Prime does not use include files so we just declare them at the top of the source file. Then, the actual source code of the main program and its subroutines are placed below.

Edited: 17 Oct 2013, 7:30 a.m.

                  
Re: setting up subroutines that are called many times...
Message #4 Posted by Geoff Quickfall on 17 Oct 2013, 1:24 p.m.,
in response to message #3 by Han

Han,

Yes, I see many similarities to basic BASIC! Thanks for the syntax on the subroutines. I will give them a try. Exporting the >24 hour subroutine makes sense as I have this as a global on all my machines..

It's just getting a working handle in the language and that comes with practice.

Nigel,

Thanks for the input, get a prime!!!

In RPN for the 42S the subroutine is labelled LBL '24H":

LBL 24H
HMS+
24
X>Y?
CLX
HMS-
RTN

In basic the intitial test for the second time having past through 24hr GMT is as follows:

10  .......
20  IF OFF>ONA THEN ONB=ONA+24 ELSE ONB=ONA
30  .......

very similar.

Now as a subroutine I would want to test for any additional additive for passing through 24hr GMT with the following and in Basic this would be:

!    PREVIOUS LINES PART OF CALCULATION RESULTING IN THE NEXT LINE
!    WHICH ADDS A PREVIOUS TIME TO A NEW TIME INTERVAL

110 TI=T1+T2 120 IF T1>24 THEN GOSUB 700 ELSE !LOOP BACK AND DO THE NEXT ADDITION

690 ....... 700 IF T1>24 THEN TI=TI-24 ELSE TI RETURN 710 .......

these two examples are called upwards of 6 times depending on the break schedule subroutine run under the program Schedule:

The actual program notes on the prime are:

Break schedules: 
-calculates break periods. 

*Initialize: - prompts for "off and on" times. these are the airborne and landing times. - break "out and in" times. these are the delay and early return times in minutes after takeoff and before landing. - number of breaks.

*equal breaks: - 2, 4, 6 typically.

*weighted breaks: - front or back. allows the front half or rear half of the breaks to be longer by choice. - 4 or 6 typically.

*2 short followed by 3 long and short: - places the sleep period outside the dinner service.

cheers and thanks for the help.

geoff

Edited: 17 Oct 2013, 2:01 p.m.

                        
Re: setting up subroutines that are called many times...
Message #5 Posted by Geoff Quickfall on 18 Oct 2013, 3:41 a.m.,
in response to message #4 by Geoff Quickfall

Ha! It works, thanks Han!

Still messing with the loops but the subroutines gets called perfectly.

Now for the finite loops.

Geoff


[ Return to Index | Top of Index ]

Go back to the main exhibit hall