Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
luofeiyu
Posts: 53 Joined: Thu Sep 14, 2017 2:11 am
Post
by luofeiyu » Mon Feb 03, 2025 8:52 am
The function have no value to return ,it print argument.
Code: Select all
function myprint(ostring)
print ostring
end function
Call the function in the main sub:
Code: Select all
sub main
myprint(ostring="welcome")
end sub
Why it print "False" ,instead of "welcome"?
Last edited by
luofeiyu on Tue Feb 04, 2025 1:50 am, edited 1 time in total.
LibreOffice 7.4.7.2 on Debian 12
robleyd
Moderator
Posts: 5383 Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia
Post
by robleyd » Mon Feb 03, 2025 9:05 am
Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 25.2.4.3; SlackBuild for 25.2.4 by Eric Hameleers
---------------------
Roses are Red, Violets are Blue]
Unexpected '{' on line 32 .
Zizi64
Volunteer
Posts: 11476 Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary
Post
by Zizi64 » Mon Feb 03, 2025 9:39 am
You need pass only the value (the string). Do not pass the name of the variable.
Code: Select all
sub main
myprint("welcome")
end sub
The ostring is a
local variable in your
function .
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2 ;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
robleyd
Moderator
Posts: 5383 Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia
Post
by robleyd » Mon Feb 03, 2025 12:32 pm
You might also want to learn about the differences between subroutines and functions; particularly that functions return a value.
Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 25.2.4.3; SlackBuild for 25.2.4 by Eric Hameleers
---------------------
Roses are Red, Violets are Blue]
Unexpected '{' on line 32 .
JeJe
Volunteer
Posts: 3064 Joined: Wed Mar 09, 2016 2:40 pm
Post
by JeJe » Mon Feb 03, 2025 12:41 pm
Because ostring="welcome" is false so you're passing false
( ostring="welcome" is evaluated and as ostring is an undeclared variable the expression is evaluated as false)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 3064 Joined: Wed Mar 09, 2016 2:40 pm
Post
by JeJe » Mon Feb 03, 2025 1:04 pm
Code: Select all
Sub Main
ostring="welcome" 'declaring it first works
myprint ostring
myprint ostring="welcome" 'then this is true
End Sub
function myprint(ostring)
print ostring
end function
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 3064 Joined: Wed Mar 09, 2016 2:40 pm
Post
by JeJe » Mon Feb 03, 2025 1:19 pm
What you're trying to do, which you can do in other languages, is assign a value when you declare a variable. You can't do that in Basic - except by being lax and not using dim at all. This gives an error, can't do it in Basic:
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)