7

I have the following code:

    Program function_as_an_array
    implicit none
    integer:: i
    integer, parameter:: N=10
    real*8:: x(N),y(N),f(N)

    do i=1,N
      x(i)=float(i)
    end do

    call func(f,N,x)

    open(unit=20, file='test.dat')
    do i=1,N
      y(i)=f(i)
      write(20,*) x(i),y(i) 
    end do
    close(20)
    Stop 
    End Program function_as_an_array


    Subroutine func(f,N,x)
    implicit none
    integer i,N
    real*8:: x(N),f(N) 

    do i=1,N
       f(i)=x(i)**2
    end do

    end Subroutine func

I want to make the program indeed be meant for "function as an arrray", i.e. I would like to replace the Subroutine func by a function f and get the same result (In the main program, I wish to keep a statement like y=f(x,N)). How can I do that?

halfer
  • 19,824
  • 17
  • 99
  • 186
hbaromega
  • 2,317
  • 2
  • 24
  • 32

2 Answers2

10

There's no problem having a function return an array, as with this question and answer: the main issue is that you need the function to be in a module (or contained within the program) so that there's an automatic explicit interface: (Edit to add: or explicitly defining the interface as with Alexander Vogt's answer)

module functions
contains

    function func(N,x)
    implicit none
    integer, intent(in) :: N
    double precision, intent(in) :: x(N)
    double precision, dimension(N) :: func

    integer :: i

    do i=1,N
       func(i)=x(i)**2
    end do

end function func

end module functions

Program function_as_an_array
use functions
implicit none
integer:: i
integer, parameter:: N=10
double precision:: x(N),y(N)

do i=1,N
  x(i)=float(i)
end do

y = func(N,x)

open(unit=20, file='test.dat')
do i=1,N
  write(20,*) x(i),y(i)
end do
close(20)
Stop
End Program function_as_an_array

But note that this sort of function - applying the same operation to every element in array - is somewhat more nicely done with a Fortran elemental function, defined to work simply on a scalar and Fortran will automatically map it over all elements of an array for you:

module functions
contains

    elemental double precision function f(x)
    implicit none
    double precision, intent(in) :: x

    f = x**2

    end function f

end module functions

Program function_as_an_array
    use functions
    implicit none
    integer:: i
    integer, parameter:: N=10
    double precision:: x(N),y(N)

    do i=1,N
      x(i)=float(i)
    end do

    y = f(x)

    open(unit=20, file='test.dat')
    do i=1,N
      write(20,*) x(i),y(i)
    end do
    close(20)
    Stop
End Program function_as_an_array

The nice thing about this is that it will now work on scalars, and arrays of any rank automatically. Wherever possible, it's good to have the compiler do your work for you.

Community
  • 1
  • 1
Jonathan Dursi
  • 50,107
  • 9
  • 127
  • 158
2

This is working for me:

Program function_as_an_array
implicit none
integer:: i
integer, parameter:: N=10
real*8 :: x(N),y(N),f(N)
interface func
  function func(x,N) result(f)
    implicit none
    integer N
    real*8:: x(N),f(N) 
  end function
end interface

do i=1,N
  x(i)=float(i)
end do

f = func(x,N)

open(unit=20, file='test.dat')
do i=1,N
  y(i)=f(i)
  write(20,*) x(i),y(i) 
end do
close(20)
Stop 
End Program function_as_an_array


function func(x,N) result(f)
implicit none
integer i, N
real*8:: x(N),f(N) 

do i=1,N
   f(i)=x(i)**2
end do

end function

You need to:

  • use result for an array-valued return variable [edit] or specify func as real*8:: func(N). See the comments for details.
  • use an explicit interface for external functions (or a module which has an implicit interface, see Jonathan Dursi's answer )

Then, you can directly assign the return value of the function to the array.

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
  • Do you formally need to use `result` in this case? I generally haven't and it has worked so far for me -- but just because gfortran/ifort allows it doesn't mean it's not required by the standard, and I've never had cause to look that closely. – Jonathan Dursi Jul 23 '14 at 13:10
  • I have come across problems without defining it some time ago... I kind of made it a habit to do so since. – Alexander Vogt Jul 23 '14 at 13:13
  • Huh - I should take a look and see what the standard says. I note that that approach is recommended [here](http://www.fortran90.org/src/best-practices.html), too. I like the result syntax anyway, I've just never quite acquired the habit of using it. – Jonathan Dursi Jul 23 '14 at 13:14
  • Well, I couldn't find anything in the standard (ch. 12.6.2.2) that enforces one form or the other. I'll change my answer... – Alexander Vogt Jul 23 '14 at 13:20
  • @AlexanderVogt and @JonathanDursi: thanks to both of you. And very interactive discussion in the comments. I was not aware of either `result` or `elemental`. Nevertheless, I feel more inclined towards @Jonathan since it didn't require an auxiliary function `func`. – hbaromega Jul 24 '14 at 01:37