- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I know that Fortran variables (by default), when passed from one subroutine to another via the argument list, are passed by reference.
Is there any difference if a Fortran user-defined type (ie, structure) variable is passed from one subroutine to another subroutine via the argument list?
In other words, is the structure still passed by reference or is a copy made? (I have always assumed it was passed by reference like any other variable).
Also, is there a difference in the declaration of arrays passed via the argument list as (*) vs. (some known value)?
For example, in the following subroutine, assume a is a structure (user-defined type) of MyType and b is an array of type real.
Subroutine foo(a, b)
Implicit None
Type(MyType) :: a <==== Is a copy made of the structure passed via the arg list or is this passed by reference?
Real :: b(*) <==== Assuming you know the dimension of b in this subroutine (say it's a global constant), what is the difference between declaring it as b(*) or b(TheKnownDimension)? Which is better?
End Subroutine foo
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
As for b(*) vs, b(known-dimension), the latter is always preferable if you know it. You could also have b(:) where the bounds would get passed in automatically, but this requires an explicit interface to be visible.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks, I'm still trying to wrap my mind around this topic...
So, say I have a user-defined type name "a" whichcontains a double dimensioned variable real member b(10,2).
I pass b in from one subroutine to another via the argument list like: Call foo (a%b(:,m)) where m is say, 2.
Inside sub foo, b is declared as Real :: b(10)
Am I passing an "array slice" of b into sub foo? (I'm assuming yes, but I would like to be sure).
This then would result in a copy of b being made? Is a copy of "a" (the containing structure)made (in my case undesirable since a is quite big and contains a lot more elements)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you had instead written a%b(m,:), then a copy would be made because the elements are not contiguous.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Just to be clear, if I had written a%b(m,:) as you say, a copy of "a" would not have been made, correct? Only a copy of "b" would be made?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page