7

Is there possibility to use indexing directly on a function's return value? Something like this:

readStr()(2:5)

where readStr() is a function which returns a character string or an array. In many other languages it is quite possible, but what about Fortran? The syntax in my example of course does not compile. Is there any other syntax to be used?

hamid attar
  • 388
  • 1
  • 8

3 Answers3

6

No, that is not possible in Fortran. You could, however, alter your function to take an additional index array that determines which elements are returned. This example illustrates this possibility using an interface to allow for an optional specification of the indices (simplified greatly thanks to the comment by IanH):

module test_mod
  implicit none

  contains

  function squareOpt( arr, idx ) result(res)
    real, intent(in)              :: arr(:)
    integer, intent(in), optional :: idx(:)
    real,allocatable              :: res( : )
    real                          :: res_( size(arr) )
    integer                       :: stat

    ! Calculate as before
    res_ = arr*arr

    if ( present(idx) ) then
      ! Take the sub-set    
      allocate( res(size(idx)), stat=stat )
      if ( stat /= 0 ) stop 'Cannot allocate memory!'

      res = res_(idx)
    else
      ! Take the the whole array    
      allocate( res(size(arr)), stat=stat )
      if ( stat /= 0 ) stop 'Cannot allocate memory!'

      res = res_
    endif

  end function
end module

program test
  use test_mod
  implicit none

  real    :: arr(4)
  integer :: idx(2)

  arr = [ 1., 2., 3., 4. ]
  idx = [ 2, 3]

  print *, 'w/o indices',squareOpt(arr)
  print *, 'w/  indices',squareOpt(arr, idx)
end program
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
  • It is a good method to let the function get an additional index parameter. This parameter could be an 'optional' parameter and I think then the two functions that you have written could be merged into one. I am going to try this. – hamid attar Feb 27 '15 at 11:01
  • 2
    To use the value of an optional variable in determining the characteristics of a function result, make the result allocatable and the characteristic deferred. – IanH Feb 27 '15 at 19:27
  • @IanH Thanks! I didn't know that... I'll change my answer. – Alexander Vogt Feb 27 '15 at 19:46
2

No.

But if it bothers you, you can write your own user defined functions and operators to achieve a similar outcome without having to store the result of the function reference in a separate variable.

IanH
  • 21,026
  • 2
  • 37
  • 59
  • thanks. I was wondering if there exist a syntax that I am missing. In this case, in line with your and Alexander @Vogt answers, I am going to let the function receive an additional 'optional' index parameter for that, when the performance could be drastically affected. – hamid attar Feb 27 '15 at 10:57
2

You can avoid declaring another variable if you use associate. Whether it is any better or clearer than a temporary variable must be decided by the user. The result has to be stored somewhere anyway.

 associate(str=>readStr())
   print *, str(2:5)
 end associate

It will not be very useful for this specific case with a potentially long string but might be more useful for other similar cases that get linked here as duplicates.