Hi,
Whilst going insane writing my "String Theory" offering (and another update to come!), I noticed the following:
When I pass a string to a sub routine, the sub routine declares it as a local variable, but if you manipulate that local string, it changes the original string that was passed to it - why?, it does not do that to INT's.
???
That really did drive me to insanity and beyond at one point, fortunately I realised what it was doing before I carried on with my string offering.
Anyway, we ALL should be aware of this!
See attached code.
Thanks,
Andy.
:)
we have just spent a bunch of time going over POINTERS and you even spent a bunch of time writing some examples so people would understand pointers better.
From the help file sub-section on subroutines:
IWBASIC defaults to passing by value for all numeric types and passing by reference for strings, UDT's, etc. The following table lists the defaults:
TYPE DEFAULT PASSED BY
INT/UINT VALUE
INT64/UINT64 VALUE
FLOAT VALUE
DOUBLE VALUE
WORD VALUE
CHAR VALUE
FILE REFERENCE
BFILE REFERENCE
MEMORY REFERENCE
STRING REFERENCE
WSTRING REFERENCE
ISTRING REFERENCE
Arrays REFERENCE
POINTER REFERENCE
COMREF REFERENCE
ANYTYPE REFERENCE
UDT REFERENCE
WINDOW REFERENCE
DIALOG REFERENCE
So, since all you were doing was sending it a memory location if you change the contents of the memory location it will show up some place else. Check out the BYVAL keyword. Nevermind I found where Sapero said BYVAL won't work on strings;)
Larry,
Firstly, it was not a critism, I love IWBasic.
Secondly, at Uni, I learned to write programs "Nigel proof", Nigel was far beyond an ldiot trust me, so please remember, I'm still learning the language.
I guess, although some users would have understood the help file, I bet the rest of us didn't.
So the bottom line is, when you pass a string to a sub, don't do anything to it, if you need to change that string, copy it to a new string and change that one instead.
Best wishes,
Andy.
:)
I didn't see a criticism anywhere
QuoteSo the bottom line is, when you pass a string to a sub, don't do anything to it, if you need to change that string, copy it to a new string and change that one instead.
You got it. The only thing I would add(for future ref) is apply that rule to anything that is passed by reference.
BTW, I've gotten bit by that before ::) ;D