RobotStudio event

Passing unlimited number of arguments in ProcCall

JohnG
JohnG
edited April 2021 in RAPID Programming
This question isn't a show stopper for me at all. I have what I'm looking to do coded in a different way right now, just could be a lot cleaner if what I'm looking to do is possible. Is there a way to create a procedure without a predefined set of arguments?

such as in Python:

def ProcToCall (**args):
    pass

One workaround I know I can do is predefine a larger list of optional arguments like below, but wouldn't be ideal for what I'm trying to achieve.

PROC ProcToCall (\VAR num Arg1, \VAR num Arg2.......)
    IF Present(Arg1) THEN
        !Do something
    ENDIF
    IF Present(Arg2) THEN
        !Do something
    ENDIF
    .......

Thanks in advance.

Comments

  • Micky
    Micky ✭✭✭
    Hello,
    you could pass an array that contains your required parameters.

    If you define the array directly in the routine call, you can pass as many parameters as you want without having to create an array as a variable previously.

    Attached is an example:
    PROC TEST1()
        ProcToCall;
        ProcToCall\Args:=[1];
        ProcToCall\Args:=[1,2,4];
      ENDPROC
    
      PROC ProcToCall(\num Args{*})
        VAR num nDim;
           !Get the number of parameters
        IF Present(Args) THEN
          nDim:=Dim(Args,1);
          IF Present(Args) AND nDim>=1 THEN
            TPWrite "Arg1: "\num:=Args{1};
          ENDIF
          IF Present(Args) AND nDim>=2 THEN
            TPWrite "Arg2: "\num:=Args{2};
          ENDIF
        ENDIF
      ENDPROC<br>
  • This is exactly what I was looking for. Thanks!