1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Return vector from function (fortran 95)

  1. Nov 11, 2012 #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 (Text):

    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
     
     
  2. jcsd
  3. Nov 11, 2012 #2

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    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 ??
     
  4. Nov 11, 2012 #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 (Text):

    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.
     
  5. Nov 11, 2012 #4

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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.
     
  6. Nov 12, 2012 #5
    I tried a different compiler, and now both versions of the code above work. That is very strange.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Return vector from function (fortran 95)
  1. Do Loops in Fortran 95 (Replies: 0)

Loading...