I'm writing a group of programs that are all going to share the same 35 lines (or so) of logic. I'd like to put this in a subroutine, but this subroutine would have to be able to call a search program specific to the calling program. I.e, if calling_prog_1 calls logic_subroutine, logic_subroutine should then call search_program_1. If calling_prog_8 calls logic_subroutine, logic_subroutine should call search_program_8. I wish I could pass the search program name from the calling program, but as far as I know, argument registers only work with numbers. I could put a select statement (select r[1] = 1, call search_program_1; =2, call search_program_2; etc), but it seems like there's probably a better solution than that.
Passing program name to subroutine?
-
TitusLepic -
September 1, 2015 at 6:39 PM -
Thread is marked as Resolved.
-
-
Do you mean TP or Karel?
-
You can do this with string registers. They are only present on newer controllers though. I think R30iA and newer.
Here is an example:
Caller program main.
Called program INDIRECT_CALLER:
Another way of doing it is to store your program name in a string register (SR[1] in the example below), and then concatenate the passed in register and store that in another string register (SR[2]) and call the program that way.
Caller program main.
Called program INDIRECT_CALLER:
In the last example SR[1] would have the string 'SEARCH_PROGRAM_' stored in it.
When INDIRECT_CALLER got to line 2, SR[2] would have ''SEARCH_PROGRAM_2' in it, and that is what would be called. -
Sergei - TP if possible, I've never worked with Karel but I'm willing to learn it if I have to.
Nation - String registers would be great, but my robot is a RJ3iB and doesn't have them. -
Write the necessary value to a data register, before calling the subroutine.
Use the register value in the subroutine, to decide which search program to call.