How can a scalar be passed to a vector (1D array) to a Fortran subroutine?

The title of your question is a bit misleading. Fortran doesn’t allow you to pass a scalar to an array. But what it DOES allow is passing a single element of an array to a routine’s array dummy argument – this is called “sequence association” in Fortran. As IanH and others have said, the following elements are automatically associated with the elements of the dummy array, up to the last element in the called routine’s actual array.

There are some restrictions on this feature, though. If the element is of a POINTER array,you can’t do this.

Going back to your title, I have seen many programs pass, say, the constant 3 to a routine where the dummy is an array. The routine only uses the first element, but this is not legal and newer compilers may detect the error and complain. One workaround for this is to turn the argument into an array by using an array constructor – for example, CALL FOO ([3]), but this works only if the value is to be read, not written.

I’ve written some blog posts on this general issue – see Doctor Fortran in “I’ve Come Here For An Argument” and Doctor Fortran in “I’ve Come Here For An Argument, Side 2”

Leave a Comment