Search code examples
recursionabaptowers-of-hanoi

Argument "n-1" of subroutine call gives a syntax error


I want to program a Tower of Hanoi solver in ABAP.

Here's my current code:

FORM SOLVE USING s1 TYPE c
             s2 TYPE c
             s3 TYPE c
             n  TYPE i.

  IF n = 1.

    "move disc from tower X to tower Y"
    WRITE:/ text-001, text-002, s1, text-003, text-002, s3.

  ELSE.

    PERFORM solve USING s1 s3 s2 n-1. "Here's my problem, n-1 doesn't work"

    PERFORM solve USING s1 s2 s3 1.

    PERFORM solve USING s2 s1 s3 n-1.

  ENDIF.

ENDFORM. 

My problem:

If I chose an USING parameter like n-1, SAP recognizes it as a (not existing) structure and throws an error to me.

Is there a way to accomplish this anyway?

Thanks a lot.


Solution

  • You need add space between operator. So you need to type as n - 1. On the other hand old version ABAP not support in-line operand as function/perform parameter. If you get error, firstly assign result to new variable then pass it as parameter.