- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
real,allocatable:: x(:),y(:)
allocate(x(4),y(4))
x=[1.,2.,3.,4.]
write(*,*)x,y
read(*,*)
end
real,intent(in):: inp(:)
integer,intent(in):: start
real xx(4)
integer:: j,n
n=size(inP)
do j=start,n
xx(j)=2.*inp(j)
enddo
Link Copied
- 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
3. My real intent was to test the on-the-fly-dimensioning of the function result as below (which seems to work!)
real,intent(in):: inp(:)
integer,intent(in):: start
real doubleAnArray(size(inp))
integer:: j,n
n=size(inP)
do j=start,n
doubleAnArray(j)=2.*inp(j)
enddo
- 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
Steve Lionel
Visual Fortran Engineering
In our last issue, the Good Doctor covered the topic of optional arguments, noting that an explicit interface was required. Since explicit interfaces seem to be a common point of confusion for those new to Fortran 90, (and some not so new), we'll cover this subject in more detail.
- Name of the procedure
- Whether it is a subroutine or function
- If a function, the result type
- Number, names, shapes and types of arguments
- Argument attributes, such as OPTIONAL and INTENT
Prior to Fortran 90, the only kind of interface was implicit, meaning that the compiler assumed that a routine call matched the actual routine - all you could do was specify the type of a function. The standard required that "the actual arguments ... must agree in order, number and type with the corresponding dummy arguments in the dummy argument list of the referenced subroutine." Not only was this error-prone, but it made it difficult to support desirable features such as optional arguments and array function results.
INTERFACE
SUBROUTINE MYSUB (A,B)
INTEGER ::A
REAL, OPTIONAL, INTENT(IN) :: B
END SUBROUTINE MYSUB
END INTERFACE
An INTERFACE block can go in the declaration section of a program unit, or can be made visible by use-association (in a MODULE that is USEd) or host-association (a program unit that contains the one that needs to see the interface.) An explicit interface also exists, without an INTERFACE block, if the routine is a contained procedure or is a module procedure in the enclosing module or a module that is use-associated (and the module procedure has not been made PRIVATE).
- If the procedure has any of the following:
- An optional dummy argument
- A dummy argument that is an assumed-shape array, a pointer, or a target
- A result that is array-valued or a pointer (functions only)
- A result whose length is neither assumed nor a constant (character functions only)
- A dummy argument that appears in a High Performance Fortran (HPF) data mapping directive. (While Visual Fortran does not support HPF functionality, it does recognize HPF syntax and checks it for errors. Our Tru64 UNIX compiler does support HPF.)
- If a reference to the procedure appears as follows:
- With an argument keyword
- As a reference by its generic name
- As a defined assignment (subroutines only)
- In an expression as a defined operator (functions only)
- In a context that requires it to be pure
- If the procedure is elemental
For more information on explicit interfaces, see Chapter 8 of the Compaq Fortran Language Reference Manual.
- 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
CVF won't link it because of the argument mismatch caused by the lack of the explicit interface.
Intel Fortran will build it but gets an access violation at run-time.
Both of these are behaviors I would expect. What are you seeing?
- 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,
Gerry T.
- 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
Fortran interfaces are for Fortran's benefit. C++ doesn't care whether they're present or not.
Thanks,
Gerry T.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have some functions that I would want to be pure, but they return an array: e.g. below, however, I do not know and have not been able to find out how to define the return as an array. Hence I get the warning "This name has not been given an explicit type."
PURE
FUNCTION GetNodeCoords(NodeNo) result (Coords)integer, intent(in) :: NodeNo
double precision :: Coords(3) !I suspect this is wrong, but I cant use Intent(out)Coords(x) = GetNodeX(NodeNo)
Coords(y) = GetNodeY(NodeNo)Coords(z) = GetNodeZ(NodeNo)RETURNENDMy interface is therefore:
PURE FUNCTION GetNodeCoords(NodeNo) RESULT(Coords)INTEGER, INTENT(IN) :: NodeNodouble precision :: Coords(3)END FUNCTION GetNodeCoords
I would have thought that I need to have the return type in, like I do with other pure functions that do not return arrays, but how do I say that I have double precision(3) being returned?
Any help of how to write this correctly appreciated
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Your declaration of the array-valued result is correct. You don't use intent when declaring a result.
Where are x, and the GetNodeX, etc. routines declared?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for you reply:
Sorry, I made a mistake in my code, I'd managed to miss out the "result (coords)" in the interface (but then add it into my "simplifed" example - Friday afternoons!).
However, you've answered my question as I was sceptical that I'd declared my array-valued result correctly, as I hadn't specified the type of the result explictly between the PURE & FUNCTION keywords.
Sorry, still learning after last using FTN77 >10 years ago and then doing C++ until recently!
Cheers for your swift response
Glyn
- 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