I'm doing some experiments with functions that return types other than integers,
and I've looked at some examples.
Strangely, it doesn't seem to work for me. What am I doing wrong in this code:
BEGIN
LOOP
IF (key(_o))
FRAME(100);
openfilename=openfile();
END
// make it stop!
IF (key(_esc))
say(test());
BREAK;
END
FRAME;
END // end of loop
END // end of begin
FUNCTION string test();
PRIVATE
string bla ="hello!";
BEGIN
RETURN (bla);
END
I would expect the say() function to print hello, but it prints a number, 34.
I know that functions return integers by default, but in this case I indicate
that it should return a string.
I've seen this code example on the wiki, and I thought it would work.
O, I get the same problem with floats and chars, they all seem to return an
integer.
Two solutitons:
import "mod_key"
import "mod_say"
import "mod_video"
global
openfilename;
end
FUNCTION string test()
private
string bla = "hello";
end
BEGIN
return bla;
end
BEGIN
set_mode(320,200,16);
LOOP
IF (key(_o))
FRAME(100);
// openfilename=openfile();
END
// make it stop!
IF (key(_esc))
say(test());
BREAK;
END
FRAME;
END // end of loop
END // end of begin
The other solution:
import "mod_key"
import "mod_say"
import "mod_video"
global
openfilename;
end
declare Function String test()
private
string bla="hello";
end
end
BEGIN
set_mode(320,200,16);
LOOP
IF (key(_o))
FRAME(100);
// openfilename=openfile();
END
// make it stop!
IF (key(_esc))
say(test());
BREAK;
END
FRAME;
END // end of loop
END // end of begin
FUNCTION string test()
BEGIN
return bla;
end
Third, and may be the best, solution:
import "mod_key"
import "mod_say"
import "mod_video"
global
openfilename;
end
declare Function String test()
end
BEGIN
set_mode(320,200,16);
LOOP
IF (key(_o))
FRAME(100);
// openfilename=openfile();
END
// make it stop!
IF (key(_esc))
say(test());
BREAK;
END
FRAME;
END // end of loop
END // end of begin
FUNCTION string test()
private
string bla="hello";
end
BEGIN
return bla;
end
Thanks, it now works.
Does it have something to do with foreward declaration perhaps?
yeah