Can a function return a custom TYPE? - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10) +---- Thread: Can a function return a custom TYPE? (/showthread.php?tid=2794) |
Can a function return a custom TYPE? - 12centuries - 06-12-2024 Hello! Former PDS 7.1 developer here, and I recently stumbled on some of my old source code files while digging through an old hard drive. That led me to qb64 and eventually here! So I decided to take a stab at completing a little text-based map scroller that I started a few decades ago, and I'm using some of the new conventions available with QB64PE. It brings me my first question: Can a function return a custom TYPE? If so, how? Consider the following code: Code: (Select All)
I get the following two errors: Expected = similar user defined type on line 10 and User defined types in expresssions are invalid on line 21 Is this telling me that I can't use TYPEd variables as return types for functions or am I missing something more obvious? RE: Can a function return a custom TYPE? - SMcNeill - 06-12-2024 You can't use User Defined Types as the function return itself, but you can use them as a parameter and pass the value back and forth. An example with your code: Code: (Select All)
As you can see, you can pass those values via parameter; just not as a return through the function itself. (Which might make it just easier to use a SUB, as I've included above as a demo for you, as well. ) RE: Can a function return a custom TYPE? - mdijkens - 06-13-2024 (06-12-2024, 11:16 PM)SMcNeill Wrote: You can't use User Defined Types as the function return itself, but you can use them as a parameter and pass the value back and forth.I would use the function and return i% when returned you might want to know if the config_item was changed RE: Can a function return a custom TYPE? - 12centuries - 06-13-2024 (06-12-2024, 11:16 PM)SMcNeill Wrote: You can't use User Defined Types as the function return itself, but you can use them as a parameter and pass the value back and forth.Ah, I completely forgot about that. It's like passing a parameter by reference in other languages. Thank you! (06-13-2024, 12:54 PM)mdijkens Wrote: I would use the function and return i%That is very good point! I was just going with "IF LEN(config_item.label) THEN" but I like your way better. Thank you! |