Functions Returning User-defined Types?


TrevorBradley

Active Member
Joined
Nov 6, 2007
Messages
732
OK, slightly different problem. This time I'm trying to get functions to return user-defined types. I'm presuming if one function is going to call another, structs won't work.

I can't get this code to compile:

CODE
Program example;

Type _mytype
String value;
End;

Function _mytype hello_world()
Private
_mytype mystring;
Begin
mystring.value = "Hello World";
return mystring;
End

Private
_mytype mystring2;
Begin
mystring2 = hello_world();
write(0,0,0,0,mystring.value);

Repeat
frame;
Until(key(_ESC))
End



Fenix gives me a compiler error: Struct Required ")" on the "mystring2 = hello_world();" line. However "hello_world()" by itself doesn't generate a compiler error.

A second question: How do you get a function to return an array? How do you assign the value of that array when you call the function? I tried pointers, but couldn't get them to work. (I still need an answer to the struct question above though...)

Any ideas? Thanks in advance!
 
Trevor Bradley said:
A second question: How do you get a function to return an array? How do you assign the value of that array when you call the function? I tried pointers, but couldn't get them to work. (I still need an answer to the struct question above though...)

CODE

Process Main()
Private
int* my_array;
Begin
//init the array
my_array = getsomeints(10);

// do stuff with ints
my_array[0] = 1;
*(my_array+1) = 2;
my_array[2] = 3;
*(my_array+3) = 4;

//output
say(my_array[3]);
say(*(my_array+3));

Repeat
frame;
Until(key(_ESC))
OnExit
//free memory used
free(my_array);
End

Function int* getsomeints(int ints)
Begin
return alloc(ints*sizeof(int));
End
And I think the other thread is about your first question.
 
Last edited by a moderator:
Back
Top