Return vector from function (fortran 95)

  • Comp Sci
  • Thread starter fortrannewbe
  • Start date
  • #1
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
 

Answers and Replies

  • #2
SteamKing
Staff Emeritus
Science Advisor
Homework Helper
12,796
1,668
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 ??
 
  • #3
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.
 
  • #4
SteamKing
Staff Emeritus
Science Advisor
Homework Helper
12,796
1,668
You may be experiencing some weird, non-standard side effect from the compiler you are using. Your program may not work reliably with another compiler.
 
  • #5
I tried a different compiler, and now both versions of the code above work. That is very strange.
 

Related Threads on Return vector from function (fortran 95)

Replies
5
Views
1K
Replies
6
Views
5K
  • Last Post
Replies
0
Views
3K
  • Last Post
Replies
3
Views
869
Replies
3
Views
4K
  • Last Post
Replies
3
Views
2K
Replies
8
Views
1K
Replies
3
Views
986
Replies
5
Views
1K
Replies
1
Views
1K
Top