Passing Scalar to Array and Vice Versa

Passing Scalar to Array and Vice Versa

Hi - I have some old code that passes scalars to functions with arrays for the dummy arguments (other parts of the code pass arrays to arrays).

For example:

real a,b
a = 3
call FUNC([a],[b],1)

real FUNC (x,y,z)
real x(z), y(z)

Thanks to the forum, I was able to get the code to compile by adding brackets to the real arguments.  However, the value that the function return (b in the example) is always zero.

Any ideas?  Is there an equivalent to the brackets that turns the array of one into a scalar?



8 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Pass one element, for example a(1).

Your example won't work, though. When you have [b] you are passing an expression that is not definable - the value you assign to y in FUNC will be thrown away.

Retired 12/31/2016

Doctor Fortran - Thanks for the reply.  I have been reading a lot of your posts.

How would you write this code?

I am not sure what you want, but I might write FUNC as an elemental function that could be used with either scalars or arrays. Something like this:

elemental function FUNC (x)
real, intent(in) :: x
FUNC = x + 3
end function FUNC

and then one could do:

b = FUNC(a)

whether a and b were scalars or arrays (have to be the same dimension arrays). An explicit interface for FUNC must be visible to the caller.

Retired 12/31/2016

Steve - After some research, I really like the idea of using an elemental.   I have a two more questions.

1 - The legacy code is has the  .f extension, so I think it is FORTRAN 77?  Can I use an elemental function?

2 - The main program passes five variables, three inputs and two outputs... so should this really be a subroutine?

Thanks for your help.


.f means fixed-form source, not FORTRAN 77.  See Doctor Fortran in "Source Form Just Wants to be Free"

If there are two outputs, then yes, a subroutine.

Retired 12/31/2016

Doctor Fortran - That helped alot.

First - There are some DATA statements in the old routine. I see that using DATA is a no-no for a n elemental routine.  What is the equvalent way to declare the variables?

For example is:

real c(3) 
data c / 7,8,9/

equvalent to

real c(3)
c(1) = 7
c(2) = 8
c(3) = 9

I read the differnce is that the DATA statment is used when it is compiled.  Does that matter?

Secondly - I get an error regarding a proceedure in an elemental proceedure:   error #7137: Any procedure referenced in a PURE procedure, including one referenced via a defined operation or assignment, must have an explicit interface and be declared PURE.

How sould internal subroutines be handeld in teh elemental subroutine?

Thanks again for all of your help.


DATA creates variables that are SAVEd across routine calls. I don't know what you're doing with this array, but if it is meant to be a constant, you could do something like this:

real, parameter :: c(3) = [7.,8.,9.]

This creates a constant array that is fine in a pure or elemental procedure.

Any procedure you call from an elemental procedure needs to have the PURE (or ELEMENTAL) attribute, and is subject to the constraints of that attribute.

It may be that ELEMENTAL is not the proper solution for your real problem, which is apparently quite different from what you originally asked.

Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today