HP Forums
example python ppl with subroutines hp prime - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: example python ppl with subroutines hp prime (/thread-17076.html)



example python ppl with subroutines hp prime - Liamtoh Resu - 06-08-2021 01:53 AM

The following demonstrate the useage of subroutines in python and ppl.
A simple pi approximation was used.

edit: fixed ppl code, was missing predeclaration for cls().

python:
Code:

#PYTHON name
# aprxpi02
import math

def pintro():
  print ("aprxpi02 pi approximation")
  return

def api():
  return 355/113

def pout():
  q1 = api()
  print ("pi = ", q1)

pintro()
pout()

#end 
 
EXPORT aprxpi02()
BEGIN
  PRINT;
  PYTHON(name);
END;

ppl
Code:

// aprxpi01
// predeclare subroutines
cls();
pintro();
api();
pout();

// main program
EXPORT aprxpi01()
BEGIN
cls();
pintro();
pout();
end;

// clear screen
cls()
begin
print();
end;

// print intro
pintro()
begin
print("aprxpi01: pi approximation");
end;

// pi approximation
api()
begin
return 355/113;
end;

// print output
local q1;
pout()
begin
q1 := api();
print("pi = " + q1);
end;

// end of source code

This is for those who are looking for a starting point.

Thanks/


RE: example python ppl with subroutines hp prime - toml_12953 - 06-08-2021 05:28 AM

(06-08-2021 01:53 AM)Liamtoh Resu Wrote:  edit: fixed ppl code, was missing predeclaration for cls().

You don't need to pre-declare functions anymore. The below still works.

Code:
// aprxpi01

// main program
EXPORT aprxpi01()
BEGIN
cls();
pintro();
pout();
end;

// clear screen
cls()
begin
print();
end;

// print intro
pintro()
begin
print("aprxpi01: pi approximation");
end;

// pi approximation
api()
begin
return 355/113;
end;

// print output
local q1;
pout()
begin
q1 := api();
print("pi = " + q1);
end;

// end of source code



RE: example python ppl with subroutines hp prime - Liamtoh Resu - 06-08-2021 05:58 AM

Ok Tom,

Thanks for the quick late night response about the pre-declaration entries.

The omission of the predeclarations did not seem to work on the emulator
--will check it out on the real hp prime.


RE: example python ppl with subroutines hp prime - toml_12953 - 06-08-2021 12:34 PM

(06-08-2021 05:58 AM)Liamtoh Resu Wrote:  Ok Tom,

Thanks for the quick late night response about the pre-declaration entries.

The omission of the predeclarations did not seem to work on the emulator
--will check it out on the real hp prime.

Are you sure you have the latest beta emulator? It works on mine without the pre-declarations.


RE: example python ppl with subroutines hp prime - Liamtoh Resu - 06-08-2021 03:45 PM

I had problems trying to get the 20210428 emulator to run so I reverted back to the
2020 emulator.
The 20210428 CK is running fine.

I have the 20210505 firmware on the real hp prime.
I have not sent the updated version to the prime yet.

PPL and python are relatively new to me -- I am thinking about
backward compatibility.

Thanks.

edit: The program without the pre-declarations worked ok on the real hp prime.