HP Forums
multiple arguments, variable arguments, default values - 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: multiple arguments, variable arguments, default values (/thread-16680.html)



multiple arguments, variable arguments, default values - Wes Loewer - 04-17-2021 01:11 PM

From the recent public beta announcement,

(04-14-2021 06:32 AM)Tim Wessman Wrote:  [*]Able to have multiple functions of same name with different number of arguments (no default arguments though)

I thought someone might benefit from seeing the syntax for this. And while technically correct that PPL does not support default arguments, you can still get the equivalent of default arguments as shown below.

Here's an example of multiple arguments using the same function name defined with different number of arguments.

Code:

// quadratic formula demonstration of default parameters
// QF(a, b, c)
// QF(b, c)  assumes a=1
// QF(c)  assumes a=1, b=0

EXPORT QF(a,b,c)
BEGIN
 LOCAL d;
 d:=b^2-4*a*c;
 RETURN (−b+{1,−1}*√(d))/(2*a);
END;

EXPORT QF(b,c)
BEGIN
  QF(1,b,c);
END;

EXPORT QF(c)
BEGIN
  QF(1,0,c)
END;


And here's a simple example of the variable argument syntax.

Code:

// Parallel Resistors: Adds resistors in parallel.
// Requires at least two parameters, a and b.
// The rest of the parameters are put in list c.
// If only two parameters are given, then c is {}.
// PPL allows up to 16 parameters total,
// so a and b plus 14 more in list c

EXPORT ParallelResistors(a,b,...c)
BEGIN
  c:=CONCAT(a,b,c);
  RETURN 1/ΣLIST(1/c);
END;

IMHO, these are nice additions to PPL.


RE: multiple arguments, variable arguments, default values - compsystems - 04-17-2021 04:32 PM

Testing the code in the simulator

QF(1,2,3) [enter] returns "Error: Invalid input"

PHP Code:
export parallelResistors_homeab, ... )
begin
  c 
:= concatab);
  return 
Σlist(1/c);
end
parallelResistors_home( 2, 4, 3 ) [enter] returns
0.92307692308 > 1 / ( ½ + ¼ + 1/3) // ok

parallelResistors_home( 2, 4 ) [enter] returns
1.33333333333...∞ > 1 / ( ½ +¼ )
exact(Ans) [enter] 4/3

parallelResistors_home( 1, 2, 4, 3, 5 ) [enter] returns
0.43795620438 > 1 / ( 1 + ½ + ¼ + 1/3 + 1/5 ) // ok


PHP Code:
#cas
parallelResistors_casab, ... ):=
begin
  c 
:= concatab);
  return 
Σlist(1/c);
end;
#end 
[Check] returns "Error: Syntax Error in program line 2"

parallelResistors_cas(R_1, R_2, R_3) > 1 / ( (1/R_1) +(1/R_2)+1/R_3) ) > R_1*R_2*R_3 / (R_1*R_2 + R_1*R_3 + R_2*R_3)


RE: multiple arguments, variable arguments, default values - Wes Loewer - 04-17-2021 05:59 PM

(04-17-2021 04:32 PM)compsystems Wrote:  QF(1,2,3) [enter] returns "Error: Invalid input"

x^2+2x+3 has imaginary roots. Go to Home Settings and turn on Complex mode to see the results.

Quote:[php]#cas
parallelResistors_cas( a, b, ... c ):=

I think the new syntax for multiple and variable arguments is only for PPL, not CAS programming.

Also, note that there is no space between the dots and the variable name: ...c not ... c


RE: multiple arguments, variable arguments, default values - cyrille de brébisson - 04-19-2021 09:50 AM

Hello,

Thanks, nice example and good test programs to make sure the system works.

indeed, these are PPL improvements, not CAS ones...

Cyrille


RE: multiple arguments, variable arguments, default values - compsystems - 04-19-2021 08:39 PM

> ... turn on Complex mode to see the results.

change flag externally

HComplex:=00; [enter] 00 // a+b*i format and complex solutions off
QF(1,2,3) [enter] returns "Error: Invalid input" > "Unreral"

HComplex:=01; [enter] 01 // a+b*i format and complex solutions on
QF(1,2,3) [enter] {−1+1.41421356238*i, −1-1.41421356238*i } [ab/c] {−1+√2*i,−1-√2*i}

HComplex:=11; [enter] 11 // a+b*i> (a,b) format and complex solutions on
QF(1,2,3) [enter] { (−1, 1.41421356238),(−1, −1.41421356238 )}

change the flag internally within a code
PHP Code:
EXPORT QF(a,b,c)
BEGIN
 LOCAL d
;
 
HComplex:=01;
 
d:=b^2-4*a*c;
 RETURN (
−b+{1,−1}*(d))/(2*a);
END