Return vector from function (fortran 95)

  • Context: Comp Sci 
  • Thread starter Thread starter fortrannewbe
  • Start date Start date
  • Tags Tags
    Function Vector
Click For Summary

Discussion Overview

The discussion revolves around the challenges of returning an array or vector from a function in Fortran 95, particularly when the function is defined within a module versus within the main program block. Participants explore different approaches and the behavior of various compilers.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a problem with returning an array from a function defined in a Fortran module, noting that it works when the function is placed inside the main program block.
  • Another participant suggests that while a function can be declared with a specific type, the function name itself may not be declared as an array, although arrays can be passed as arguments.
  • A different participant argues that it is indeed possible to declare a function as an array, providing an example where the function works when defined in the main program block, but expresses concern about code organization.
  • One participant speculates that the issue might be related to non-standard behavior from the compiler being used.
  • A later reply indicates that trying a different compiler resolved the issue, leading to both versions of the code working, which raises questions about compiler reliability.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best way to declare functions returning arrays, and there are competing views on the behavior of different compilers regarding this issue.

Contextual Notes

Participants express uncertainty about the implications of compiler differences and the organization of code when using functions in Fortran. There are unresolved questions about the standardization of function declarations in relation to arrays.

fortrannewbe
Messages
3
Reaction score
0
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
 
Physics news on Phys.org
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.
 
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.
 
I tried a different compiler, and now both versions of the code above work. That is very strange.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K