Post Reply 
Optional Parameter in User Function.
06-14-2021, 09:03 PM (This post was last modified: 06-14-2021 09:10 PM by matalog.)
Post: #1
Optional Parameter in User Function.
Is there anyway to set an optional paramater in a User created function?

I use the following all day, to convert feet to inches:

Code:
EXPORT F2M(A,B)
BEGIN
 C:=A/3.28084;
 D:=B/39.3701;
 E:=C+D;
 RETURN ROUND(E,2);
END;

I press a user defined key that types into F2M() for me, and leaves the cursor in the brackets, which is very convenient.

Quite often, I do not need to input any inches, and therefore would like to be able to set B as an optional parameter/variable in the function, where the function will default B to Zero when it is not input. The function will currently not run if I do not enter a value for A, and a value for B. F2M(6,2) is an accepted input, but F2M(10) will not work, and I would like it to, instead of having to type F2M(10,0).
Find all posts by this user
Quote this message in a reply
06-14-2021, 09:24 PM (This post was last modified: 06-14-2021 11:24 PM by StephenG1CMZ.)
Post: #2
RE: Optional Parameter in User Function.
Not in the current product, but I understand this is possible in the beta release.
Currently, one workaround is to use lists:

Outline only:
Export feet(LST)
If size(LST) ==1...LST(1)
IF SIZE(LST)== 2...LST(1)+LST(2)
Call: feet({5}) or feet({5,6}

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
06-14-2021, 09:28 PM
Post: #3
RE: Optional Parameter in User Function.
(06-14-2021 09:24 PM)StephenG1CMZ Wrote:  Not in the current product, but I understand this is possible in the beta release.

I think I am running the beta release, is it documented anywhere?
Find all posts by this user
Quote this message in a reply
06-14-2021, 09:40 PM (This post was last modified: 06-14-2021 09:46 PM by Albert Chan.)
Post: #4
RE: Optional Parameter in User Function.
We could use complex numbers ... (just think of i as inches)

Code:
EXPORT F2M(z)
BEGIN
  ROUND((re(z)*12+im(z))*0.0254, 2);
END;

> F2M(6+2i)       → 1.88
> F2M(10)          → 3.05
Find all posts by this user
Quote this message in a reply
06-14-2021, 09:56 PM (This post was last modified: 06-14-2021 09:56 PM by matalog.)
Post: #5
RE: Optional Parameter in User Function.
(06-14-2021 09:40 PM)Albert Chan Wrote:  We could use complex numbers ... (just think of i as inches)

Code:
EXPORT F2M(z)
BEGIN
  ROUND((re(z)*12+im(z))*0.0254, 2);
END;

> F2M(6+2i)       → 1.88
> F2M(10)          → 3.05

That allows what I am asking for, but it makes it quite a bit less convenient to enter feet and inches, it now requires 6 + 2 (shift) 2 (5 keys). Before it just required typing 6 , 2 (just 3 keys).

I am grateful for your input, but this is about increasing convenience, and reducing the total amount of keys required to press, because of how often the function is used, therefore, it isn't really a viable solution.
Find all posts by this user
Quote this message in a reply
06-14-2021, 09:58 PM (This post was last modified: 06-14-2021 10:02 PM by Didier Lachieze.)
Post: #6
RE: Optional Parameter in User Function.
(06-14-2021 09:28 PM)matalog Wrote:  
(06-14-2021 09:24 PM)StephenG1CMZ Wrote:  Not in the current product, but I understand this is possible in the beta release.

I think I am running the beta release, is it documented anywhere?

From the additional documentation referenced here:
Quote:Multiple functions with the same names, but different parameter count
It is now possible to have more than one function in a program with the same name, as long as the parameter count is different.

So you can have two functions F2M(A,B) and F2M(A), with:

Code:
EXPORT F2M(A)
BEGIN
 F2M(A,0);
END;

This way F2M(6,2) or F2M(10) will both work.
Find all posts by this user
Quote this message in a reply
06-14-2021, 10:08 PM
Post: #7
RE: Optional Parameter in User Function.
(06-14-2021 09:56 PM)matalog Wrote:  That allows what I am asking for, but it makes it quite a bit less convenient to enter feet and inches, it now requires 6 + 2 (shift) 2 (5 keys). Before it just required typing 6 , 2 (just 3 keys).

Can you setup user defined key to produce "F2M((" ?

> F2M((6,2       → 1.88
> F2M((10        → 3.05
Find all posts by this user
Quote this message in a reply
06-14-2021, 10:10 PM
Post: #8
RE: Optional Parameter in User Function.
(06-14-2021 09:58 PM)Didier Lachieze Wrote:  
(06-14-2021 09:28 PM)matalog Wrote:  I think I am running the beta release, is it documented anywhere?

From the additional documentation referenced here:
Quote:Multiple functions with the same names, but different parameter count
It is now possible to have more than one function in a program with the same name, as long as the parameter count is different.

So you can have two functions F2M(A,B) and F2M(A), with:

Code:
EXPORT F2M(A)
BEGIN
 F2M(A,0);
END;

This way F2M(6,2) or F2M(10) will both work.


That's exactly what I want, thanks a lot.
Find all posts by this user
Quote this message in a reply
06-15-2021, 03:24 AM
Post: #9
RE: Optional Parameter in User Function.
Here's a thread that details both methods of using optional arguments:
https://www.hpmuseum.org/forum/thread-16680.html
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)