IonicWind Software

IWBasic => General Questions => Topic started by: tekrat on November 14, 2007, 11:43:49 AM

Title: Return an ISTRING from a Subroutine?
Post by: tekrat on November 14, 2007, 11:43:49 AM
The program crash error when I try to return an ISTRING from by subroutine.

SUB MakeBug(), STRING

DEF RETURN_VALUE[2000]:ISTRING
FOR X = 1 to 1999
RETURN_VALUE = APPEND$(RETURN_VALUE, "*")
NEXT X
RETURN  RETURN_VALUE

ENDSUB

OPENCONSOLE

JUNK = MakeBug()
PRINT JUNK

PRINT "Press Q to quit"
DO: UNTIL INKEY$ = "Q"
CLOSECONSOLE


I've tried to making my subroutine and ISTRING but of course that errors at compilation.  Any ideas?

SUB MakeBug(), ISTRING
Title: Re: Return an ISTRING from a Subroutine?
Post by: Ionic Wind Support Team on November 14, 2007, 12:03:20 PM
You are using autodefinition of the string JUNK.   Which means since you didn't define it your self the compile makes it a STRING type whose length is 255 characters.  And returning a 2000 character string will definately overwrite it and another 1745 bytes of memory your program doesn't own ;)

So the answer is to define your variables


SUB MakeBug(), STRING

DEF RETURN_VALUE[2000]:ISTRING
FOR X = 1 to 1999
RETURN_VALUE = APPEND$(RETURN_VALUE, "*")
NEXT X
RETURN  RETURN_VALUE

ENDSUB

OPENCONSOLE

DEF JUNK[2000] as ISTRING
JUNK = MakeBug()
PRINT JUNK

PRINT "Press Q to quit"
DO: UNTIL INKEY$ = "Q"
CLOSECONSOLE