[Solved] OPTIONAL keyword on SUB statement

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
mcalex
Posts: 4
Joined: Sun Apr 02, 2023 8:47 am

[Solved] OPTIONAL keyword on SUB statement

Post by mcalex »

Hi all,

I'm confused by the use of the Optional keyword in Sub definitions. I have tried to look through documentation (but you all know that story. :D ) and may have found something out of date or correct for a different version. In any case, my understanding of syntax that should be correct is roughly:
`Optional` varName `As Type = ` value
so for example:

`Optional msg As String = "Hello world"` *

When I try this in a LO macro, the compiler says: "BASIC syntax error. Expected )." and highlights the equals sign.

Undaunted, I remove the offending bit (' = "Hello world"'), leaving the 'Optional' keyword and trying to incorporate the check into the sub

`Optional msg As String`
...
if (msg = "") then
msg = "Hello world"
end if` **

The compiler then tells me: "BASIC runtime error. Argument is not optional." and selects the line with the if statement.
Can someone point me to what I'm doing wrong (or where the correct syntax is :-) )?


Full method code:
* Ex 1:

Code: Select all

Sub TestOptional(Optional msg As String = "Hello world")
  msgbox msg

End Sub
** Ex 2:

Code: Select all

Sub TestOptional(Optional msg As String)
  if (msg ="") then
    msg = "Hello World"
  end if
  msgbox msg

End Sub
 Edit: Changed subject, was [Solved] Optional keyword 
Make your post understandable by others 
-- MrProgrammer, forum moderator 
Last edited by MrProgrammer on Sun Apr 02, 2023 8:21 pm, edited 2 times in total.
Reason: Edited topic's subject
LibreOffice Latest (coz) ArchLinux
User avatar
robleyd
Moderator
Posts: 5087
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Optional keyword

Post by robleyd »

The Optional Parameters section of Procedures and Functions seems to hold the solution to your problem.

Spoiler: The IsMissing function checks whether a parameter has been passed or is left out.
 Edit: Ack, you are using LibreOffice - see https://wiki.documentfoundation.org/Doc ... Parameters. Same story, different book. 
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
mcalex
Posts: 4
Joined: Sun Apr 02, 2023 8:47 am

Re: Optional keyword

Post by mcalex »

Thank you for the very quick response. That looks like it all makes sense. Ready to call it solved, but for bonus internet points, what on earth is https://help.libreoffice.org/7.5/en-US/ ... bPAR=BASIC talking about? Is that not supposed to be official doco for LO? I am so happy for having an explanation, but still quite confused.

In any event, I will stick to the documentfoundation info from now on.

Thank you
LibreOffice Latest (coz) ArchLinux
User avatar
robleyd
Moderator
Posts: 5087
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Optional keyword

Post by robleyd »

I'm not sure what you are asking here. Yes, that is official LO Help documentation; the link I gave is to a Wiki maintained by The Document Foundation which is the home of inter alia LibreOffice.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Keyword "Optional"

Post by Lupp »

Most specifications concerning native Basic are still valid for LibreOffice and OO.o/AOO as well due to the common origin.

The one field where LibreOffice Basic actually is much more developed than AOO Basic is support for VBA code. In addition there are some bugfixes.

An OPINION of mine concerning the way information is shown by the link you doubt: Such syntax diagrams are only a part of the trendy "visualisitis". They pretend to present things lucidly, but are usually more difficult to read/understand and much harder to remember than traditional descriptions, They can even compete insofar with Munich Subway overviews. The trend, however, is about 40 of age, meanwhile. Strange.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
mcalex
Posts: 4
Joined: Sun Apr 02, 2023 8:47 am

Re: Optional keyword

Post by mcalex »

I should have been more explicit.

My confusion stems from the fact the syntax diagram for Optional params in the LO documentation on that page says that the syntax I attempted in OP should be correct. Following the (badly drawn) path 'Optional msg As String = "Hello world"' appears valid, yet according to the compiler it isn't.
opArg.png
opArg.png (23.43 KiB) Viewed 3965 times
Have I read the diagram wrong?
LibreOffice Latest (coz) ArchLinux
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Optional keyword

Post by Lupp »

Partly one of the obscuring effects I complained about.
MAINLY: The linked page IMO is only applicable to modules with

Code: Select all

Option VBAsupport 1
Native LibO/AOO Basic neither support on-the-fly default-assignments nor the keyword "ParamArray".
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [Solved] Optional keyword

Post by Lupp »

Though topic "[SOLVED]" an additional hint:
There are (so I was told; don't know a reliable list) constructs well accepted by native LibO Basic and also known to VBA, but working in a slighly(?) different way depending on whether VBA is supported or not.

To avoid new surprises, confusion, and errors, I restrict "Option VBAsupport 1" mostly to one module containing routines with names starting with "vba" These routines only contain code with functionality for which VBA support is actually needed. Done the required preparations I then call from those vbaNamed routines others (without the "vba" in the name) placed in a module without the critical option, and therefore reliably executing statements as LibO Basic should do. Results go back to the vbaNamed routine. If called from a Calc formula the routine (function) can now pass a return value for the calling expression.

What are the cases where VBA has features sufficiently advantageous to justify the described effort?
I see three reasons in some cases:
1. Optional parameters can get assigned default values en-passant (originally at the heart of this topic).
2. Calls from Calc formulas can pass range (A-Range) objects supporting service "ooo.vba.excel.Range" to functions.
3. VBA supports the ParamArray clause used with the final parameter of the routine declaration.

'2.' or '3.' are strong reasons in some cases. '1.' isn't. It's just a comfortable add-on if one of the other reasons anyway requires VBA support.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
cwolan
Posts: 129
Joined: Sun Feb 07, 2021 3:44 pm

Re: Optional keyword

Post by cwolan »

mcalex wrote: Sun Apr 02, 2023 1:19 pm My confusion stems from the fact the syntax diagram for Optional params in the LO documentation on that page says that the syntax I attempted in OP should be correct. Following the (badly drawn) path 'Optional msg As String = "Hello world"' appears valid, yet according to the compiler it isn't.

Have I read the diagram wrong?
Look at the description below the diagram:
--------------------------------
eq expression.jpg
eq expression.jpg (61.08 KiB) Viewed 3831 times

Insert

Code: Select all

Option Compatible
into your code and call your sub.

Code: Select all

TestOptional()
TestOptional("Goodbye world")
OpenOffice 1.1.5 – 4.1.15
LibreOffice 3.3.0.4 – 7.6.6
Windows 7,10,11 64-bit
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] OPTIONAL keyword on SUB statement

Post by Villeroy »

Python is by far less confusing than StarBasic while being a fully qualified scripting language for adults. Basic is not relevant anymore. LibreOffice supports Python as an alternative macro language.

Python code

Code: Select all

def routine(a, b, c, d="foo", *args):
defines arguments a, b, c, optional d with a default value "foo" and an optional parameter array.
There is no difference between subs and functions, no trouble with data types empty, nothing, null and missing (there is just Null). You can use double-quotes within single quoted strings and single quotes within double-quoted strings. The language has dozens of built-in functions dealing with arrays and strings.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: [Solved] OPTIONAL keyword on SUB statement

Post by Lupp »

I would like to express my belief that adults also have the right to prefer smaller toolboxes . and especially in the case of advanced age are allowed to prefer not to learn and practice the use of a huge number of importable tool shelves, but to work with what they know. Especially if one finds the proposed material also unappealing, a postponement to the next life lends itself.
I am definitely not a follower of Basic, but also not one of denigrating dissenting opinions as childish.
(Tired of reading stuff like this over and over again.)
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
JeJe
Volunteer
Posts: 2784
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] OPTIONAL keyword on SUB statement

Post by JeJe »

I'll just add an important addition to Lupp's uses for VBA support/Compatible... it supports class modules.

Python enables you to do some things which can't be done in Basic. But Basic has the IDE... and a big code base (OO code and VB6 code much of which can translate, and forum answers etc.)

Edit: Each to their own...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
mcalex
Posts: 4
Joined: Sun Apr 02, 2023 8:47 am

Re: [Solved] OPTIONAL keyword on SUB statement

Post by mcalex »

Asked and answered,
and answered
and answered.

Many thanks @robleyd, @Lupp, @cwolan, @JeJe, @Villeroy for the help and excellently opinionated commentary. :bravo:
LibreOffice Latest (coz) ArchLinux
User avatar
karolus
Volunteer
Posts: 1160
Joined: Sat Jul 02, 2011 9:47 am

Re: [Solved] OPTIONAL keyword on SUB statement

Post by karolus »

Lupp wrote: Sun Apr 02, 2023 10:36 pm I would like to express my belief that adults also have the right to prefer smaller toolboxes . and especially in the case of advanced age are allowed to prefer not to learn and practice the use of a huge number of importable tool shelves, but to work with what they know. Especially if one finds the proposed material also unappealing, a postponement to the next life lends itself.
I am definitely not a follower of Basic, but also not one of denigrating dissenting opinions as childish.
(Tired of reading stuff like this over and over again.)
yes, everyone is allowed to use what they like, but it is childish to react so insultingly to any objective comparison of language possibilities.
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
User avatar
karolus
Volunteer
Posts: 1160
Joined: Sat Jul 02, 2011 9:47 am

Re: [Solved] OPTIONAL keyword on SUB statement

Post by karolus »

Villeroy wrote:

Code: Select all

def routine(a, b, c, d="foo", *args):
not exactly, the arguments must be in order: 1st:positional, 2nd:*optional , 3rd:**keyword_arguments, so your Example shoud read:

Code: Select all

def routine(a, b, c, *args, d="foo"):
…
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Post Reply