Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Subroutine passing value

Former Member
0 Likes
1,210

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

11 REPLIES 11
Read only

Former Member
0 Likes
1,178

Dear Ahmad,

   Where you defined zemp. How you are getting value in it.

Read only

0 Likes
1,178

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.

Read only

0 Likes
1,178

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

Read only

Former Member
0 Likes
1,178

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

Read only

Former Member
0 Likes
1,178

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

Read only

matt
Active Contributor
0 Likes
1,178

Point of order. That's not the syntax for remote form call. It's the syntax for dynamically calling a form in another program.

Read only

0 Likes
1,178

picky picky.....

Read only

matt
Active Contributor
0 Likes
1,178

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".

Read only

0 Likes
1,178

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

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
1,178

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

Read only

matt
Active Contributor
0 Likes
1,178

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.