Bennu Game Development

English Forums => Helpdesk => Topic started by: handsource-dyko on November 25, 2010, 03:32:52 PM

Title: function that should return some other type than an int, returns an int
Post by: handsource-dyko on November 25, 2010, 03:32:52 PM
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.
Title: Re: function that should return some other type than an int, returns an int
Post by: DCelso on November 25, 2010, 04:51:13 PM
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


Title: Re: function that should return some other type than an int, returns an int
Post by: DCelso on November 25, 2010, 05:07:22 PM
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

Title: Re: function that should return some other type than an int, returns an int
Post by: handsource-dyko on November 25, 2010, 06:21:50 PM
Thanks, it now works.

Does it have something to do with foreward declaration perhaps?
Title: Re: function that should return some other type than an int, returns an int
Post by: DCelso on November 25, 2010, 08:12:12 PM
yeah