Page 1 of 1

QBasic function with optional arguments

Posted: Thu Dec 07, 2023 8:37 am
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?

Re: QBasic function with optional arguments

Posted: Sat Nov 22, 2025 9:35 pm
by Peter Swinkels
No, qb doesn't support optional arguments. What are you trying to do?

Re: QBasic function with optional arguments

Posted: Tue Feb 17, 2026 6:24 pm
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 3613 times

Re: QBasic function with optional arguments

Posted: Tue Mar 31, 2026 2:52 pm
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.