# Return vector from function (fortran 95)

• Comp Sci
Hi,
i have some problems to return a array or vector from a function that is located in a fortran-module. If i put the function inside the main program block with a "contains" it works, but why does the following not work? Does anyone have an idea?

Thanks
Code:
module testmodule

contains

function testfunction(a)

real, dimension(3) :: testfunction
real :: a

testfunction(1) = a + 10
testfunction(2) = a + 20
testfunction(3) = a + 30

end function testfunction

end module testmodule

program testprogram

use testmodule

real :: b = 15

print*, testfunction(b)

end program testprogram

Related Engineering and Comp Sci Homework Help News on Phys.org
SteamKing
Staff Emeritus
Homework Helper
I believe a function can be declared of a certain type (real, integer, complex, etc.), but I don't think the function name itself can be declared as an array. Arrays, however, can be passed to the function as arguments.

real :: testfunction is OK

real, dimension (3) :: testfunction ??

But I think it is possible to declare a function as an array, because it works when I do it in the following order:

Code:
program testprogram

use testmodule

real :: b = 15

print*, testfunction(b)

contains

function testfunction(a)

real, dimension(3) :: testfunction
real :: a

testfunction(1) = a + 10
testfunction(2) = a + 20
testfunction(3) = a + 30

end function testfunction

end program testprogram
But this is a very horrible way to use the program language, because the program looks disarranged if there are many functions.

SteamKing
Staff Emeritus