[solved] no value to function or sub

New to FreeBASIC? Post your questions here.
Post Reply
exagonx
Posts: 247
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

[solved] no value to function or sub

Post by exagonx »

Hello guys, I have a question:

lately I found myself in conditions where a certain function could be used with or without value but during the compilation if I don't specify an empty string or a zero it gives me an error, is there a way to ignore the absence of value between the brackets?

Code: Select all

sub x(byval value as integer)

if value < 0 then print "no value" else Print "the value are: " & value
end sub

x(20) ' with value
x() ' without value
Last edited by exagonx on Jan 24, 2022 11:19, edited 1 time in total.
paul doe
Moderator
Posts: 1634
Joined: Jul 25, 2017 17:22
Location: Argentina

Re: no value to function or sub

Post by paul doe »

exagonx wrote: Jan 24, 2022 10:59 Hello guys, I have a question:

lately I found myself in conditions where a certain function could be used with or without value but during the compilation if I don't specify an empty string or a zero it gives me an error, is there a way to ignore the absence of value between the brackets?
Optional parameters. Just assign a default value to the parameter, like this:

Code: Select all

sub x(byval value as integer=0)

if value < 0 then print "no value" else Print "the value is: " & value
end sub

x(20) ' with value
x() ' without value
exagonx
Posts: 247
Joined: Mar 20, 2009 17:03
Location: Italy
Contact:

Re: no value to function or sub

Post by exagonx »

paul doe wrote: Jan 24, 2022 11:10 Optional parameters. Just assign a default value to the parameter, like this:
thank you Work perfectly

Code: Select all


' adding = -1  give a default value
sub x(byval value as integer = -1)

if value < 0 then print "no value" else Print "the value are: " & value
end sub

x(20) 'with value
x() 'with no value

Post Reply