HP Forums
Can programs have optional arguments? - 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: Can programs have optional arguments? (/thread-9017.html)



Can programs have optional arguments? - webmasterpdx - 09-09-2017 01:56 AM

Is it possible to write programs with optional arguments with default values? If so, what is the syntax?
Thx
-Donald


RE: Can programs have optional arguments? - Carlos295pz - 09-09-2017 02:52 AM

It is not possible, it is very related to define polymorphic functions and that is not possible in HP PPL, the alternatives can be to create 2 independent functions, or send a parameter that determines the different type of action you want to have on the data sent.


RE: Can programs have optional arguments? - Joe Horn - 09-09-2017 04:09 AM

Another option, although awkward, is possible: Make the input a string, and then parse the string yourself in your program.


RE: Can programs have optional arguments? - webmasterpdx - 09-09-2017 05:13 AM

Thank you


RE: Can programs have optional arguments? - Tyann - 09-09-2017 03:34 PM

Bonjour

J'ai déja fait ce genre de choses en utilisant une liste pour
transmettre/recevoir les paramètres.



Hello

I've already done this sort of thing using a list for
transmit / receive parameters.


RE: Can programs have optional arguments? - webmasterpdx - 09-09-2017 10:07 PM

I'm thinking like a function that has:
func(a,b,[c]), like some of the library functions....
You can create 2 functions but you can't do it like this:
EXPORT func(a,b,c=0)
...

I don't see how a list lets you do this...?
Thx
-D


RE: Can programs have optional arguments? - Tyann - 09-10-2017 05:35 AM

Bonjour, Hello

Par exemple :
For exemple :

Code:

Export Func(p)
BEGIN
Local a,b,c
a:=p(1);b:=p(2);
IFERR c:=p(3) THEN c:=0; END;
....
....
END;

Func({10,15,2}) -> c=2
Func({10,15}) -> c=0

C'est un peu artisanal mais cela fonctionne.
It's a little crafty but it works.

Si il y a plusieurs arguments optionnels et d'autres obligatoires vous pouvez mixer.
If there are several optional arguments and mandatory others you can mix.

Code:

EXPORT Func(a,b,p)
BEGIN
LOCAL c,d,e
IF TYPE(p)==6 THEN
 c:=p(1);d:=p(2);e:=p(3)
ELSE
 c:=p;d:='def value';e:='def value';
END;
...
...
END;

Espérant vous avoir aidé.
Hoping you helped.


RE: Can programs have optional arguments? - cyrille de brébisson - 09-11-2017 05:13 AM

Hello,

PPL does not support this for user functions. sorry.
You can "emulate" it (as suggesteed before) either by creating 2 distinct functions (func2 and func3, for respectivly 2 and 3 args for example) and/or by the use of a list of input parameters.

Cyrille


RE: Can programs have optional arguments? - parisse - 09-11-2017 04:52 PM

Giac has support for default parameters, for example
Code:
f(x=3):=x*x; f(); f(5)
but it is probably too recent to be in the CAS of the current HP firmware.


RE: Can programs have optional arguments? - webmasterpdx - 09-11-2017 08:24 PM

Good!