Forum
Function to edit a string (*solved*) - Printable Version

+- Forum (https://www.boriel.com/forum)
+-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12)
+--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11)
+---- Forum: Bug Reports (https://www.boriel.com/forum/forumdisplay.php?fid=15)
+---- Thread: Function to edit a string (*solved*) (/showthread.php?tid=994)



Function to edit a string (*solved*) - patters - 12-29-2020

I'm having trouble passing a string to a Function in order to edit it. The code which works fine outside of a Function just won't modify the string inside the Function for some reason. Any idea what I'm doing wrong?
Code:
DIM a$ AS String

FUNCTION editStringFN(stringToEdit$ AS String, position AS Integer, newLetter$ AS String) AS String
    stringToEdit$(position)=newLetter$
    RETURN stringToEdit$
END FUNCTION

CLS
a$="sample"
PRINT a$; "- original string"

'try to edit string via the function
a$=editStringFN(a$, 1, "i")
PRINT a$; "- function fails to edit"

'same edit without using the function
a$(1)="i"
PRINT a$; "- works as regular code"



RE: Function to edit a string - patters - 12-29-2020

Hmm. Judging from the wiki documentation, ByREF seems to be what I need here, but it doesn't seem to help. The example usage is with arrays not strings. Is there a way to achieve what I'm trying to do here?
The reason I wanted to use a function is that I have several sprites stored as strings, and I want to edit multiple sprites in fundamentally the same way - hence the function to avoid code repetition.


RE: Function to edit a string - boriel - 12-30-2020

Yes, you need to use ByRef if you want the variable passed to the SUB / Function to be changed. Otherwise, you're passing a copy of the value. ByRef also works with strings and numeric variables.
Try and tell me, please. Rolleyes


RE: Function to edit a string - patters - 12-30-2020

No, it doesn't seem to work. See example - the function won't edit the string:
Code:
DIM a$ AS String

FUNCTION editStringFN(ByREF stringToEdit$ AS String, pos AS Integer, newLetter$ AS String) AS String
    stringToEdit$(pos)=newLetter$
    PRINT AT 10,0; stringToEdit$
    RETURN stringToEdit$
END FUNCTION

CLS
a$="sample"
PRINT a$; "- original string"

'try to edit string via the function
a$=editStringFN(a$, 1, "i")
PRINT AT 1,0;a$; "- function fails to edit"

'same edit without using the function
a$(1)="i"
PRINT a$; "- works as regular code"



RE: Function to edit a string - boriel - 12-30-2020

Ok. This looks like a regression bug that compiler tests failed to detect! Sad
This is *CRITICAL*. Will fix it ASAP.

One more thing: If you pass it byref you don't need to return the value since it's already changed.


RE: Function to edit a string - boriel - 01-02-2021

Okay, this beta fixes that bug. Please, try it and tell me if it works Rolleyes :

http://www.boriel.com/files/zxb/zxbasic-1.14.0-beta9.tar.gz
http://www.boriel.com/files/zxb/zxbasic-1.14.0-beta9.zip
http://www.boriel.com/files/zxb/zxbasic-1.14.0-beta9-win32.zip
http://www.boriel.com/files/zxb/zxbasic-1.14.0-beta9-linux64.tar.gz
http://www.boriel.com/files/zxb/zxbasic-1.14.0-beta9-macos.tar.gz


RE: Function to edit a string - patters - 01-02-2021

This fixes the BYREF issue with my simple test program I posted above in this thread. However, it makes my game code unstable and crashes the Spectrum. Will send the code via PM.


RE: Function to edit a string - patters - 01-03-2021

I had originally been returning the amended string from the FUNCTION using RETURN, which is no longer necessary when using BYREF.
I had deleted the RETURNed result from my code, but I forgot that I had declared the function AS STRING, which Boriel quickly spotted.
I needed to delete that from the end of the FUNCTION declaration, since I was no longer returning anything. Once that was done the code was stable. 

To avoid this error condition I presume a compiler check is needed for FUNCTION type declarations when nothing is being RETURNed. Perhaps it should require such a FUNCTION to be changed to a SUB instead. I've done this in my code since I noticed the compiler was defaulting the FUNCTION type to float when the declaration was removed.


RE: Function to edit a string - boriel - 01-03-2021

(01-03-2021, 12:44 AM)patters Wrote: I had originally been returning the amended string from the FUNCTION using RETURN, which is no longer necessary when using BYREF.
I had deleted the RETURN from my code, but I forgot that I had declared the function AS STRING, which Boriel quickly spotted.
I needed to delete that from the end of the FUNCTION declaration, since I was no longer returning anything. Once that was done the code was stable. 

To avoid this error condition I presume a compiler check is needed for FUNCTION type declarations when nothing is being RETURNed. Perhaps it should require such a FUNCTION to be changed to a SUB instead. I've done this in my code since I noticed the compiler was defaulting the FUNCTION type to float when the declaration was removed.

Yes, I'm working on it. This is a bit harded than it sounds as I was planning to start code-analysis for v1.15 onwards. Anyway, I'll add little code analysis now.