2016 Jan 05 10:01 AM
As a beginner, I have created a subroutine and want to use in another program by calling value.
Please advice the code Am i right here ..
FORM ZX using P .
IF zemp BETWEEN '00000001' AND '00088888'.
SELECT SINGLE * FROM P100 WHERE pernr = zemp
AND subty = 'A'.
desig = P100-desig_desc.
P = desig.
ELSE.
SELECT SINGLE * FROM p200 WHERE pernr = zemp.
desig = p200-desig.
P = desig.
ENDIF.
ENDFORM. " ZX
Please advise the above code for subroutine is fine .or advise the same...
After that i have used the above subroutine in below form in another prog
PERFORM zx(zprogrma) using P.
Please advise how to pass a value here in subroutine.
Regards
Ahmad
2016 Jan 05 10:19 AM
Dear Ahmad,
Where you defined zemp. How you are getting value in it.
2016 Jan 05 10:43 AM
thanks..
zemp in the same subroutine ...
i have assigned a value desig here... and now i want to use this value in other prog by using subroutine pass by value.
Please check the code in a question ..
please let me know how to us in the other program.
2016 Jan 05 10:50 AM
Hi,
Yes, but Zemp is not referenced before you use it in the If statement so it will have no value. You need to define desig as a parameter in the form definition and then use that in your perform statement.
Note that 'pass by value' parameters are generally used for read only parameters where the value will not be changed.
Rich
2016 Jan 05 10:25 AM
Hi
If you wish to use routines in mulitple programs it would be better to either create a function group and modules or create a class and methods.
If you do wish to call a subroutine in one program from another program you should (and in any case always) make that subroutine totally independant. ie everything it uses in terms of variables etc must be passed in as parameters rather than be dependant on any global parameters of the host program.
It is also a very good idea to 'TYPE' your parameters. This has two benefits. The first is that it prevents you the programmer from making coding mistakes and secondly it avoids implied data conversions which eat up processing time. (Not much for a routine called a few times but it soon mounts up when it can be called 1000's of times...)
The parameters of your suboutine should actually be 'CHANGING' rather than 'USING' since you are changing the value of P in your subroutine.
To assign a value to P before you call the subrutine just use the assignment operator '=':
P = 'Hello'.
Regards
Rich
2016 Jan 05 10:53 AM
Hi Ahmad,
Find syntax for calling subroutine of another program.
DATA: l_subroutine TYPE string,
l_program TYPE string.
l_subroutine = 'SUBROUTINE'.
l_program = 'Z_PROGRAM'.
perform (l_subroutine) in program (l_program) using p.
Make sure subroutine and program name given in uppercase.
Thanks and Regards,
Pavan Kumar
2016 Jan 05 11:05 AM
Point of order. That's not the syntax for remote form call. It's the syntax for dynamically calling a form in another program.
2016 Jan 05 11:10 AM
2016 Jan 05 11:16 AM
If people aren't accurate in their definitions, it leads to confusion. Confusion and IT (while very common) isn't terribly desirable.
RC 0.2 = "well, it nearly worked".
2016 Jan 05 12:03 PM
Thanks All,
Dear pavan,
You have written the code syntax in the program where i am calling the subroutine..
please advise me what should be the code syntax in subroutine..as o have written here below...
How to define P that contain desig value which is to be passed in another program.is this right way here
DATA : zemp like p100-pernr,
designation LIKE p200-desig_desc.
DATA : P(10) .
FORM ZX using P .
IF zemp BETWEEN '00000001' AND '00088888'.
SELECT SINGLE * FROM P100 WHERE pernr = zemp
AND subty = 'A'.
desig = P100-desig_desc.
P = desig.
ELSE.
SELECT SINGLE * FROM p200 WHERE pernr = zemp.
desig = p200-desig.
P = desig.
ENDIF.
ENDFORM. " ZX
Regards,
Ahmad
2016 Jan 05 11:01 AM
Calling subroutines of a program from other programs is obsolete and bad style. Create a global class with a public method instead ...
Message was edited by: Horst Keller
2016 Jan 05 11:04 AM
1) Remote performs are to be avoided as much as possible.
2) FORM/PERFORM as a modularisation technique is now consider obsolete - use classes/methods or function modules.
3) Type your parameters. Untyped parameters lead to hard-to-find runtime errors.
4) Don't use global variables. Pass all you need in the parameters.