1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
// tutorial.hpc -- Summary of the basic hp48cc syntax -*- C -*-
// You can insert the code right there; you will
// not need a main() function.
// Push the string onto the stack and call the built-in
// function MSGBOX.
MSGBOX("Hello");
// Declare a new variable and assign a value.
foo = 1;
// Increment variable value.
++foo;
// Declare a new variable.
bar = foo + 5;
// Call the HP48 built-in function PURGE to remove
// the two variables. @{} is the HP48 built-in list.
PURGE(@{foo, bar});
// -------------------------------------------------------------------
// Define a new function MYSUM with two paramenters.
declare mysum(a, b) {
// Create a new variable and assign the sum
// of a and b.
s = a + b;
// Push the value of s into the stack
s;
// Remove s from HP48 variables.
PURGE(@{s});
}
// The previous function is exactly equal to the
// following, but this is faster.
declare mysum2(a, b) {
// Push the sum into the stack
a + b;
}
// We defined two functions, now we can call them.
a = mysum(1, 2);
b = mysum2(4, 5);
c = "The value of a is " + a + ".";
d = "The value of b is " + b + ".";
MSGBOX(c);
MSGBOX(d);
// Clean up
PURGE(@{mysum, mysum2, a, b, c, d});
// -------------------------------------------------------------------
// If you wish, you can specify the variables local to the
// function. Local variables have automatically assigned a value and
// are automatically removed at function exit.
// Local variables are really useful and should be used instead
// of hand-defined variables (as in function mysum), because:
// * Variables defined by hand and then PURGEd may collide with
// external variables;
// * If the function causes an error, variables defined by hand
// are not removed;
// * Local variables always have a value assigned;
// * No need to call PURGE;
// * Programs using local variables are faster.
declare area(h, w): a(0) {
a = h * w; // Calculate area
a; // Return value
}
MSGBOX("Area of 23x45: " + area(23, 45));
PURGE(@{area});
// -------------------------------------------------------------------
// Define a new function CKSUM that returns
// a sorta of checksum of the argument.
declare cksum(s) : k(0) {
// Force string type; uses STR-> built-in
s = \\\-\>STR(s);
while (SIZE(s) > 0) {
k += NUM(HEAD(s));
s = TAIL(s);
}
k; // Return value
}
MSGBOX("Checksum 1: " + cksum("Hello"));
MSGBOX("Checksum 2: " + cksum("Foo"));
MSGBOX("Checksum 3: " + cksum(@{a, b, c}));
// Clean up
PURGE(@{cksum});
// -------------------------------------------------------------------
// Calculate the distance between (x0, y0) and (x1, y1).
declare distance(x0, y0, x1, y1) {
// \\v\/ is traslated by hp48cc to \v/, than translated
// by HP48 into the square root symbol (see README for more info).
// x**y means x raised to y.
\\v\/((x1 - x0)**2 + (y1 - y0)**2);
}
MSGBOX("Distance between (2, 3) and (5, 7): " + distance(2, 3, 5, 7));
// Clean up
PURGE(@{distance});
|