I get used to JAVA, thus having problem to write this code in ABAP.
I call a method with two parameters. It should return a number, so I can save it.
What I want is
int result = generate_num(40,5);
int generate_num(int thisNum, int newDigit){
return thisNum * 10 + newDigit;
}
In ABAP I tried this so far.
//Declared Method
methods GENERATE_NUM
importing
!thisNum type I
!NEWDIGIT type DIGIT_NUMBER_VALUE.
//Calling Method
CALL METHOD me->Generate_NUM
EXPORTING
thisNUm = 40
newDigit = 5.
//Method itself
METHOD GENERATE_NUM.
DATA: newNum type i.
If thisnum < 0.
newNum = thisnum * 10 - newdigit.
Else.
newNum = thisnum * 10 + newdigit.
ENDIF.
RETURNING VALUE(newNum).
ENDMETHOD.
But I get lost in this code, have no idea how to return a value and how to save it in another variable.
That’s how you declare a method with a return parameter:
Note that a method can only have one
RETURNINGparameter, and that parameter must always be passed by value.In the method implementation, you set the return value by modifying the local variable you declared as returning parameter:
The returned value will be whatever value
resulthas when the method returns. Just like withEXPORTINGparameters.When you call a method you can either use the classic CALL syntax which is more like the syntax seasoned ABAP developers are used to:
or the functional syntax which is more like the Java syntax you might be used to:
If the method doesn’t just have importing parameters but also changing or exporting parameters, the syntax looks like this: