![]() |
|
Returning a String from a function - Printable Version +- Boriel Basic Forum (https://forum.boriel.com) +-- Forum: Compilers and Computer Languages (https://forum.boriel.com/forumdisplay.php?fid=12) +--- Forum: ZX Basic Compiler (https://forum.boriel.com/forumdisplay.php?fid=11) +---- Forum: Help & Support (https://forum.boriel.com/forumdisplay.php?fid=16) +---- Thread: Returning a String from a function (/showthread.php?tid=321) |
Returning a String from a function - LTee - 2011-03-03 Hi all! Is it possible to return a string from a function? I tried this: Code: dim result as STRINGBut I get a compile error: Code: test.bas:2: Cannot convert value to string. Use STR() functionRe: Returning a String from a function - boriel - 2011-03-03 LTee Wrote:Hi all! Your code looks ok to me. This seems a reintroduced bug. Let me check it...Can you compile against v1.2.6? Re: Returning a String from a function - LTee - 2011-03-04 Oops! I just gave it a go with 1.2.6 r1812 (the final release of 1.2.6?) and it throws the same compilation error. Here's the interesting thing - the call to the function only fails to compile if it's within the same file as the function declaration. So if I make two files, like this: ReturnString.bas: Code: function test(n as UBYTE) as STRINGTestReturnString.bas: Code: #include "ReturnString.bas"... and then compile TestReturnString.bas, then everything is fine. I guess this explains why some of the library files compile okay, they have String functions in them but never call themselves. :-) Re: Returning a String from a function - LTee - 2011-03-04 Ah! Further to this, it appears that successful (or not!) compilation actually depends on where the call is in the file. If the call appears BEFORE the declaration then compilation will fail. But if it appears AFTER the declaration then compilation succeeds. i.e. this version won't compile: Code: dim result as STRING... but this remixed version will actually work okay: Code: function test(n as UBYTE) as STRINGThis only seems to be true for String functions - functions that return numerics appear to work fine regardless of where the call is. Re: Returning a String from a function - boriel - 2011-03-04 Okay, I remember. It's not a bug. If you call a function in advance (not being declared yet) the compiler will asume it's a FLOAT function. You have to use DECLARE, to declare the function header in advance (like in C): Code: declare function test(n as Ubyte) as StringRe: Returning a String from a function - LTee - 2011-03-04 Ah, gotcha. That seems simple enough - thanks for the help! I got out of the habit of having to declare functions in advance since I started doing Java rather than C for work. :-) Re: Returning a String from a function - LTee - 2011-03-06 Okay, I'm still not quite there... apologies! :-) I'm having strange problems when I declare functions in advance. Code which would previously compile starts giving 'undefined label' compilation errors once I declare a function. For instance, this segment of a high score routine compiles okay: Code: 'table arraysHowever, I can't call hsGetName unless I declare it, so I changed the code to include a line for that: Code: 'table arraysNow when I try to compile I get this: Code: test.bas:51: Error: Undefined label '_hsName'The longer piece of code that this comes from generates similar but slightly different compilation errors (sometimes the label names are not ones that I've used in the code), and the line numbers reported do not always point to relevant lines of code (e.g. sometimes they point to lines which only have comments on them). Am I missing something in the declare statement? This is still with 1.2.7, btw. Re: Returning a String from a function - boriel - 2011-03-10 Ok, this is another *NASTY* bug. Fixed now. Sorry. :oops: Download new release 1.2.8r2153, here: <!-- m --><a class="postlink" href="http://www.boriel.com/wiki/en/index.php/ZX_BASIC:Archive#Latest_Development_Version">http://www.boriel.com/wiki/en/index.php ... nt_Version</a><!-- m --> 1.2.7 is *flawed*. I'm going to remove it from the download page. It's obvious I was impatient to add new features instead of making more testing. 1.2.8 is much better. I won't add new features from a while (in 1.2.8). It has some major code rearrangements (which might introduce new bugs). So better stay here until the compiler is well tested before going into 1.2.9 with Dynamic arrays (or the whatever). Re: Returning a String from a function - LTee - 2011-03-10 Well, that seems to work perfectly now! The only errors left are in my own code. :mrgreen: Many, many thanks, boriel! Re: Returning a String from a function - boriel - 2011-03-10 Ok. This bug is close (Well, this bunch of them). Still a -O3 bug pending, BTW :? |