Bad Values When QLB Function Used as SUB Parameter (69157)






This article was previously published under Q69157

SUMMARY

If a FUNCTION returning a floating-point value is located in a Quick library and is directly used as the second, third, or later parameter of a SUB or FUNCTION in a program, then incorrect values may be passed to the SUB or FUNCTION being called. This problem occurs only in the QuickBasic environment.

Microsoft has confirmed this problem with Microsoft QuickBasic versions 4.00, 4.00b, and 4.50. This problem does not exist with the Microsoft Basic Professional Development System (PDS) versions 7.00 and 7.10.

When compiled and linked (with the FUNCTION in a .LIB library) into an .EXE file, the same program will run correctly.

A simple workaround for this problem is to assign a temporary variable to the return value of the Quick library function, then pass that temporary variable when you invoke the SUB or FUNCTION.

MORE INFORMATION

The results may vary depending upon the number and type of parameters passed to the FUNCTION within the Quick library. If the Quick library function accepts parameters, bad values may be passed to the calling SUB or FUNCTION. However, if no parameters are accepted by the Quick library function, a machine hang may occur when that function is used as a parameter to another SUB or FUNCTION.

Using the debug options when creating the Quick library, or using the ON ERROR GOTO ... RESUME lines in either the main program or in the Quick library, does not eliminate the problem. The code below demonstrates the problem.

The following function goes in the Quick library:
DECLARE FUNCTION func1! (x!)
FUNCTION func1! (x!)
    func1! = x! * 2
END FUNCTION
				
The following program invokes the Quick library's function directly in the parameter list of the SUB mysub, and demonstrates the problem:
DECLARE FUNCTION func1! (x!)
DECLARE SUB mysub (a!, b!)
CALL mysub (100, func1! (45))
END
SUB mysub (a!, b!)
PRINT a!; b!  'This should print 100 90, but incorrectly prints 90 90
END SUB
				
A simple modification using a temporary variable (y!) in the calling program works around the problem, as shown below:
DECLARE FUNCTION func1! (x!)
DECLARE SUB mysub (a!, b!)
y! = func1! (45)
CALL mysub (100, y!)
END
SUB mysub (a!, b!)
    PRINT a!; b!
END SUB
				

Modification Type: Minor Last Reviewed: 1/9/2003
Keywords: KB69157