function that should return some other type than an int, returns an int

Started by handsource-dyko, November 25, 2010, 03:32:52 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

handsource-dyko

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.

DCelso

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


Monstruos Diabólicos

"A PAck of classic GAMEs For BennuGD" en desarrollo
http://code.google.com/p/apagame4be/

DCelso

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

Monstruos Diabólicos

"A PAck of classic GAMEs For BennuGD" en desarrollo
http://code.google.com/p/apagame4be/

handsource-dyko

Thanks, it now works.

Does it have something to do with foreward declaration perhaps?

DCelso

Monstruos Diabólicos

"A PAck of classic GAMEs For BennuGD" en desarrollo
http://code.google.com/p/apagame4be/