Programming Style - Passing Variables to Subroutines 
Author Message
 Programming Style - Passing Variables to Subroutines

Dear Netters,

I am not sure what the better programmng style is in fortran90. Nor-
mally, variables in Fortran are passed by reference, i.e. one should
declare them as in method # 1. However, when I just want to pass in-
formation to the subroutine (the INTENT(IN) case), I tend to prefer
the more lenient way which just passes the value expected by the sub-
routine (method # 2). Both ways work with the IBM XL Fortan compiler.

What do you think is the better style? Should I put the line
   INTEGER, INTENT(IN) :: flag
into the subroutine's code when applying method # 2 to make it safe?

Thanks in advance for your advice,
ulf

method # 1:

   PROGRAM main
     ...
     i_flag = 1
     CALL sub_foo (i_flag)
     ...
   END PROGRAM main

   SUBROUTINE sub_foo (i_flag)
     ...
   END SUBROUTINE sub_foo

method # 2:

   PROGRAM main
     ...
     CALL sub_foo (1)
     ...
   END PROGRAM main

   SUBROUTINE sub_foo (i_flag)
     ...
   END SUBROUTINE sub_foo

--
Ulf Mazurek           e-mail: ulf at www dot chem dot tu-berlin dot de

Usual disclaimers apply.
2 + 2 = 5 for extremely large values of 2.



Fri, 18 Aug 2000 03:00:00 GMT  
 Programming Style - Passing Variables to Subroutines

Quote:

> Dear Netters,

> I am not sure what the better programmng style is in Fortran90. Nor-
> mally, variables in Fortran are passed by reference, i.e. one should
> declare them as in method # 1. However, when I just want to pass in-
> formation to the subroutine (the INTENT(IN) case), I tend to prefer
> the more lenient way which just passes the value expected by the sub-
> routine (method # 2). Both ways work with the IBM XL Fortan compiler.

> What do you think is the better style? Should I put the line
>    INTEGER, INTENT(IN) :: flag
> into the subroutine's code when applying method # 2 to make it safe?

...

You should certainly use INTENT(IN) when that is your intent, but
it is unlikely to affect the method your compiler uses to pass
variables.

You should use "INTENT(IN)" in the subroutine in both of your
examples, not just "method # 2", if the subroutine does not
alter tha value of the variable.  (Of course, in your "method # 2"
example, you would be violating the Fortran standard if you tried
to alter i_flag within the subroutine, with or without an INTENT
specified for it.)

        -P.

--
************** In memoriam, Grandpa Jones, 1913-1998, R.I.P.
**************
* Peter S. Shenkin; Chemistry, Columbia U.; 3000 Broadway, Mail Code
3153 *

***
*MacroModel WWW page:
http://www.columbia.edu/cu/chemistry/mmod/mmod.html *



Fri, 18 Aug 2000 03:00:00 GMT  
 Programming Style - Passing Variables to Subroutines

On Mon, 02 Mar 1998 20:39:18 +0100, Ulf Mazurek

Quote:

>I am not sure what the better programmng style is in Fortran90. Nor-
>mally, variables in Fortran are passed by reference, i.e. one should
>declare them as in method # 1.

Wrong.  All Fortran standards from (at the very least) F77 to F95
allow passing numerical constants as arguments to subprograms.  Both
of your methods should be fine with every Fortran compiler you'll ever
be likely to meet.  May I assume that you are a C/C++ programmer? <g>
It is _not_ necessary to provide any special code of your own for the
"pass by reference" mechanism to work.

Fortran is a high-level language, not a thinly disguised macro
assembler, so you don't need to do the compiler's work such as
selecting calling methods yourself. <g>  Any Fortran compiler will
always produce correct code, regardless of whether you write
sub_foo(i_flag) or sub_foo(1).  (Btw: I gather from the description of
Lahey's -pca switch that a call by reference is generated even for
sub_foo(1), at least with no optimisations in effect!)

Regarding INTENT() statements, the best way is to use the appropriate
form ((IN), (OUT) or (IN OUT)) for each and every argument of each and
every subprogram.  INTENT() statements provide semantic checks at
compile time and possibly lead to more efficient object code.  Again,
this is completely independent of whether you supply variables,
Fortran "parameters" (aka symbolic constants) or numerical constants
to the subprogram in question -- the compiler handles these details.
--

Remove NOSPAM to reply via e-mail



Fri, 18 Aug 2000 03:00:00 GMT  
 Programming Style - Passing Variables to Subroutines

Many thanks to everyone who replied.

ulf



Tue, 22 Aug 2000 03:00:00 GMT  
 Programming Style - Passing Variables to Subroutines


        >On Mon, 02 Mar 1998 20:39:18 +0100, Ulf Mazurek

        >>I am not sure what the better programmng style is in Fortran90. Nor-
        >>mally, variables in Fortran are passed by reference, i.e. one should
        >>declare them as in method # 1.

        >Wrong.  All Fortran standards from (at the very least) F77 to F95
        >allow passing numerical constants as arguments to subprograms.  Both
        >of your methods should be fine with every Fortran compiler you'll ever
        >be likely to meet.

I think what Ulf Mazurek is concerned about it the accidental
destruction of a constant, if passed as an argument.

If the called procedure makes an assignment to the
dummy argument (either by accident or design),
the actual argument is changed!

If the procedure is called from the same statement,
a different actual argument is used instead of the
original constant argument.

If other statements happen to use that same constant
(courtesy of the compiler), then those statement
will compute wrong results!

        > May I assume that you are a C/C++ programmer? <g>
        >It is _not_ necessary to provide any special code of your own for the
        >"pass by reference" mechanism to work.

Yes, but if the subroutine assigns to the dummy argument,
the argument is corrupted (with dire consequences when
the arument is a constant).  So beware!!

        >=46ortran is a high-level language, not a thinly disguised macro
        >assembler, so you don't need to do the compiler's work such as
        >selecting calling methods yourself. <g>  Any Fortran compiler will
        >always produce correct code, regardless of whether you write
        >sub_foo(i_flag) or sub_foo(1).  (Btw: I gather from the description of
        >Lahey's -pca switch that a call by reference is generated even for
        >sub_foo(1), at least with no optimisations in effect!)

        >Regarding INTENT() statements, the best way is to use the appropriate
        >form ((IN), (OUT) or (IN OUT)) for each and every argument of each and
        >every subprogram.  INTENT() statements provide semantic checks at
        >compile time and possibly lead to more efficient object code.  Again,
        >this is completely independent of whether you supply variables,
        >=46ortran "parameters" (aka symbolic constants) or numerical constants
        >to the subprogram in question -- the compiler handles these details.



Fri, 25 Aug 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Passing logical variables from Fortran subroutine to C subroutine

2. Character variable variable passed as a subroutine argument

3. Q. Passing variable dimensioned arrays to subroutines.

4. passing functions or subroutines in a subroutine

5. pass a subroutine (use derived data type) as an argument to another subroutine

6. passing an allocatable matrix in a subroutine to the main program

7. Programming Style - Parameter passing or use of modules

8. Subroutine inexplicably changes variables in main program.

9. Dimensioning in a subroutine with variables from main program

10. Pass an AWK variable to a pipeline program as a parameter

11. passing shell variable into awk program

12. how to pass on variables to another program

 

 
Powered by phpBB® Forum Software