PRINT TAB Can Change Value of Parameter in Subprogram (41536)






This article was previously published under Q41536

SYMPTOMS

The code below demonstrates that a variable that has been passed to a subprogram procedure may incorrectly be changed by a PRINT TAB(n) statement. However, the same statement in the main module does not cause the error.

The problem can be avoided by not using the PRINT TAB(n) statement, or by assigning a local variable to the passed parameter in the SUB and using the local variable in the PRINT TAB(n) statement, or by compiling with the BC /X option. (The /X compiler switch is normally used only to flag an ON ERROR statement with RESUME, RESUME NEXT, or RESUME 0.)

STATUS

Microsoft has confirmed this to be a bug in an .EXE program compiled with BC.EXE in QuickBasic version 4.5. No problem occurs in the QB.EXE environment. This problem was corrected in Microsoft Basic Professional Development System (PDS) version 7.1 (fixlist7.10).

MORE INFORMATION

The program example below works correctly with all versions of QuickBasic earlier than version 4.5.

Code Example

DECLARE SUB testsub (row%)
row% = 9
' this line is ok -- it is in the main module
LOCATE row%, 1: PRINT "main line row%= "; row%; TAB(38); row%;
row% = 10
CALL testsub(row%)
END

SUB testsub (row%)
  LOCATE row%, 1: PRINT "Sub row%= "; row%;
  ' The value of row% prints incorrectly:
  PRINT TAB(38); "row now = "; row%
END SUB
				
To work around the problem in QuickBasic 4.5, print a local variable instead of the passed parameter, for example:
SUB testsub (row%)
  x%=row%
  LOCATE row%, 1: PRINT "Sub row%= "; row%;
  PRINT TAB(38); "row now = "; x%      ' x% prints ok.
END SUB
				

Modification Type: Minor Last Reviewed: 1/8/2003
Keywords: KB41536