1

I'm using a Fortran function declared with array type. The problem is that the compiler doesn't preserve the bounds of arrays, for example, I declared

function normalize(vector)

  real,dimension(0:)::vector
  real,dimension(0:size(vector)-1)::normalize
  real scale
  integer dim

  scale=norm_real(vector)
  if (scale/=0) then
    do dim=0,size(vector)-1
      normalize(dim)=vector/scale
    end do
  end if

  write(*,*) Lbound(normalize,1)
end function normalize

...

real, dimension(:), allocatable :: B

B = normalize(vector)

write(*,*) Lbound(B,1)

In this case I verify the Lbound of the function normalize and it's 0, yet it's Lbound In main program or in another side is 1. So how can I preserve it's Lbound?

Nouri.M
  • 31
  • 1
  • 4
  • You should show more code ans show how you diagnose the problem. How do you print the lbound? – Vladimir F Героям слава Sep 03 '16 at 08:43
  • 1
    This is one of the very few times when pointers are your friend. When you pass arrays as arguments to subroutines, the upper and lower bounds of the actual argument are not maintained in the call unless the lower bound is 1. However, when passing pointers, the lower and upper bounds are maintained. Please read http://stackoverflow.com/questions/38140951/fortran-subroutine-returning-wrong-values/38154225?noredirect=1#comment63750238_38154225 – jlokimlin Sep 03 '16 at 16:42
  • 1
    @jlokimlin If he used a subroutine argument then allocatable would be enough. However, the question is about a function result, not about a subroutine argument. I agree it is not written very clearly. The comment to M.S.B.'s answer explains it somewhat. – Vladimir F Героям слава Sep 03 '16 at 17:03

1 Answers1

1

I don't think that you can, but on the other hand that it doesn't matter. Fortran considers array shapes to be the more fundamental property. Even though the lbound of the function return becomes one, you can assign it to an array with lbound of 0, or any other value. Do you have a case where it matters?

M. S. B.
  • 28,968
  • 2
  • 46
  • 73
  • Yes there is no problem,if I save the value of the function in an array with 0 Lbound. However in long fortran code, we need some time to pass function with array type as an argument in other function,(to avoid the use of additional variable) or to allocate automatically an allocatable with it's value since it's allowed in fortran 2003.(for example B=normalize(vector)). where B is in this case a dynamic array. – Nouri.M Sep 03 '16 at 13:22
  • This answer is correct, you cannot. If you want to allocate to a specific lower bound, you can use a subroutine. You cannot use a function. It is still not totally clear to me what you want to achieve in the end. – Vladimir F Героям слава Sep 03 '16 at 14:26
  • @VladimirF The question, is how to preserve the shape of a function returning an array type, since it's Lbound change when I call it in main program. – Nouri.M Sep 03 '16 at 16:03
  • @Nouri.M The answer is that it is not possible. – Vladimir F Героям слава Sep 03 '16 at 16:40
  • @VladimirF Thank you for replay, I hope it will be realizable in next fortran version. – Nouri.M Sep 03 '16 at 16:47