QBasic function with optional arguments

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators: Administrator, Global Moderator

Post Reply
skibidiQ
Newbie
Posts: 3
Joined: Mon Dec 04, 2023 6:37 am

QBasic function with optional arguments

Post by skibidiQ »

Is it possible to write a subroutine or a function with optional arguments?

for example:

Code: Select all

MyFunct (x AS INTEGER, [y AS INTEGER, Z AS INTEGER]) 
so that I can use the function with only the first argument and sometimes with all arguments, in the same program, like the following for example:

Code: Select all

Myfunct 5
MyFunct 5,6,7
Also, is it possible to write a sub or a function that takes arguments in format that is used with some of the graphics commands in QuickBasic, like the WINDOW and LINE.. For example:

Code: Select all

WINDOW (10,50)-(100,100) 
LINE (10,10)-(50,50
I tried using square brackets, [], because the manual said those represent the optional arguments, but I guess it is only for reading the syntax conventions. Is there any way to have optional arguments?
Peter Swinkels
Jr. Member
Posts: 19
Joined: Fri Sep 17, 2021 12:08 pm
Location: The Netherlands
Contact:

Re: QBasic function with optional arguments

Post by Peter Swinkels »

No, qb doesn't support optional arguments. What are you trying to do?
Merlin
Newbie
Posts: 1
Joined: Thu Feb 12, 2026 5:56 pm

Re: QBasic function with optional arguments

Post by Merlin »

Passing a variable number of parameters can be done.
BUT NOT in standard QuickBasic!
You'll have to do some extra programming yourself.
I once wrote an input function that was relying on more than one parameter to control how the input was to be treated for being valable. These parameters were not all/allways needed. So they were 'optional'.

If you want to use multiple parameters, you can pass the different parameters as one string value, separated by some character like a comma or a pipe for instance.
In the function or sub you need to split that string value into a string array.
Then use the array values to do your work with the 'parameters'.
You'll have to make them the correct type to use them effectively.
So it is possible.
I attached an example with a Split subroutine.
SPLITTST.BAS
(1.81 KiB) Downloaded 3612 times
Peter Swinkels
Jr. Member
Posts: 19
Joined: Fri Sep 17, 2021 12:08 pm
Location: The Netherlands
Contact:

Re: QBasic function with optional arguments

Post by Peter Swinkels »

Okay, so you can sort of simulate optional parameters. However, I wouldn't recommend write a massive amount of code that will likely hamper performance as well unless absolutely necessary.
Post Reply