Microsoft KB Archive/45483

From BetaArchive Wiki

Article ID: 45483

Article Last Modified on 11/21/2006

This article was previously published under Q45483

SUMMARY

When using FUNCTIONs or SUBprograms that are located in a Quick library under QuickBasic Versions 4.00, 4.00b, and 4.50 and under QuickBasic Extended in Basic PDS Version 7.00, it is important to DECLARE all Quick library routines that your program will be CALLing. If fewer parameters than expected are passed to a SUBprogram in a Quick library, your machine may hang, sometimes requiring the power to be cycled to reboot the machine. This problem occurs only within the environment and only when CALLing a Quick library routine.

If the program is CALLing a routine in another module that is loaded into the QB.EXE environment, the expected error of "Argument count mismatch" displays. When compiled to an EXE file, the error "Illegal function call" displays.

Microsoft does not consider this to be a problem with QuickBasic or QBX.EXE of any version. The environment cannot perform parameter checking without a DECLARE statement for each SUB or FUNCTION. Therefore a DECLARE statement is required for each routine in a Quick library in order for a program to function normally.

MORE INFORMATION

If SUBprograms are not DECLAREd at the top of the module that makes the calls, the SUBroutine must be CALLed. If you have a DECLARE SUB for that SUBroutine, you need only to mention the SUBprograms followed by any expected parameters.

When using FUNCTIONs, whether in another module or a Quick library, the FUNCTION must be DECLAREd at the top of the calling module. If the FUNCTION is not DECLAREd, the QuickBasic environment interprets the FUNCTION as an array.

For more information on SUBprograms and FUNCTIONs, consult Chapter 2 of the "Microsoft QuickBasic 4.5: Programming in Basic" manual for Microsoft QuickBasic Version 4.50 or Chapter 2 of the "Microsoft Basic 7.0: Programmer's Guide" for Microsoft Basic PDS Version 7.00.

The code example below illustrates the necessity of the DECLARE statement. If the FUNCTION and SUBroutine are combined into a Quick library and only one of the arguments is passed to the SUBprogram, the computer hangs. If both arguments are passed, it executes as expected. If the DECLARE FUNCTION is removed, a "Subscript out of range" is generated when the FUNCTION is referenced.

Code Example

Main Program

DECLARE FUNCTION calculatesomething%(t AS INTEGER)
DEFINT A-Z
t = 100
x = 100                     'to hang machine, change CALL statement to:
CALL printhello(t, x)       'CALL printhello(t)
a = calculatesomething(t)
PRINT a
                

Quick Library Routines

SUB printhello(t AS INTEGER, x AS INTEGER)
    PRINT "Hello from the SUBprogram: "; t, x
END SUB
                
FUNCTION calculatesomething%(t AS INTEGER)
    calculatesomething% = t + t * t
END FUNCTION
                


Additional query words: QuickBas

Keywords: KB45483