[Solved] Called function doesn't print argument

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
luofeiyu
Posts: 53
Joined: Thu Sep 14, 2017 2:11 am

[Solved] Called function doesn't print argument

Post by luofeiyu »

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
User avatar
robleyd
Moderator
Posts: 5383
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Why the called function can't print argument?

Post by robleyd »

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
.
User avatar
Zizi64
Volunteer
Posts: 11476
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Why the called function can't print argument?

Post by Zizi64 »

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.
User avatar
robleyd
Moderator
Posts: 5383
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Why the called function can't print argument?

Post by robleyd »

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

Re: Why the called function can't print argument?

Post by JeJe »

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

Re: Why the called function can't print argument?

Post by JeJe »

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

Re: Why the called function can't print argument?

Post by JeJe »

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:

Code: Select all

dim ostring="welcome"
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Locked