Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access elements of returned array in Fortran

How does one access an element of an array that is returned from a function? For example, shape() returns an array of integers. How does one compare an element of that array to an integer? The following does not compile:

integer :: a
integer, dimension(5) :: b

a = 5
if (a .eq. shape(b)) then
    print *, 'equal'
end if

The error is:

if (a .eq. shape(c)) then
    1
Error: IF clause at (1) requires a scalar LOGICAL expression

I understand that this is because shape(c) returns an array. However, accessing an element of the array does not appear to be possible like so: shape(c)(1)

Now if I add these two lines:

integer, dimension(1) :: c
c = shape(b)

...and change the if clause to this:

if (a .eq. c(1)) then

... then it works. But do I really have to declare an extra array variable to hold the return value of shape(), or is there some other way to do it?

like image 576
pattivacek Avatar asked Jul 24 '13 20:07

pattivacek


People also ask

Can a Fortran function return an array?

Unlike C and C++, Fortran can handle returning arrays and composite types with no difficulty.

What does Allocatable do in Fortran?

Purpose. The ALLOCATABLE attribute allows you to declare an allocatable object. You can dynamically allocate the storage space of these objects by executing an ALLOCATE statement or by a derived-type assignment statement. If the object is an array, it is a deferred-shape array or an assumed-rank array.

How do I allocate a matrix in Fortran?

You need an explicit interface to use allocatable arrays as arguments; best is to have the subroutine in a module. In particular with Jonathan Dursi's link, note the requirement for an explicit interface. This appears missing from the main program.

What is dimension statement in Fortran?

The DIMENSION statement specifies the number of dimensions for an array, including the number of elements in each dimension. Optionally, the DIMENSION statement initializes items with values.


1 Answers

Further to the answers that deal with SHAPE and logical expressions etc, the general answer to your question "How does one access an element of an array that is returned from a function?" is

  • you assign the expression that has the function reference to an array variable, and then index that array variable.

  • you use the expression that has the function reference as an actual argument to a procedure that takes a dummy array argument, and does the indexing for you.

Consequently, the general answer to your last questions "But do I really have to declare an extra array variable to hold the return value of shape(), or is there some other way to do it?" is "Yes, you do need to declare another array variable" and hence "No, there is no other way".

(Note that reasonable optimising compilers will avoid the need for any additional memory operations/allocations etc once they have the result of the array function, it's really just a syntax issue.)

The rationale for this particular aspect of language design is sometimes ascribed to a need to avoid syntax ambiguity and confusion for array function results that are of character type (they could potentially be indexed and/or substringed - how do you tell what was intended?). Others think it was done this way just to annoy C programmers.

like image 191
IanH Avatar answered Oct 26 '22 02:10

IanH