Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to initialize a parameter array in fortran?

This works just fine:

  program main
    integer,parameter,dimension(3) :: x = [1,2,3]
    print*,x
  end program main

As does this:

  program main
    integer,parameter,dimension(3) :: x = (/1,2,3/)
    print*,x
  end program main

Is there a reason to think that one form should be preferred over the other (e.g. backward compatibility)?

like image 286
mgilson Avatar asked Jan 28 '13 19:01

mgilson


People also ask

How do I initialize an array in Fortran?

For multidimensional (rank>1) arrays, the Fortran way for initialization differs from the C solution because in C multidimensional arrays are just arrays of arrays of etc. In Fortran, each rank corresponds to a different attribute of the modified data type. But there is only one array constructor, for rank-1 arrays.

What is parameter in Fortran?

Fortran Parameters. A Fortran PARAMETER defines a named constant. If your compiler generates debug information for parameters, they are displayed in the same way as any other variable. However, some compilers do not generate information that TotalView can use to determine the value of a PARAMETER.

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 are arrays stored in Fortran?

Fortran stores higher dimensional arrays as a contiguous sequence of elements. It is important to know that 2-dimensional arrays are stored by column. So in the above example, array element (1,2) will follow element (3,1). Then follows the rest of the second column, thereafter the third column, and so on.


1 Answers

The square bracket form was added to the language in Fortran 2003. If you are writing to Fortran 90 (as per the tag on the question) then the square bracket form is a syntax error (square brackets are not in the Fortran 90 character set).

Beyond language standard it a question of personal preference and style.

like image 109
IanH Avatar answered Oct 19 '22 07:10

IanH