Return vector from function (fortran 95)

In summary, there is a discussion about declaring a function as an array or passing an array as an argument to a function in Fortran. One person believes that a function cannot be declared as an array, while another person has found a way to do it. Different compilers may also affect the functionality of the code.
  • #1
fortrannewbe
3
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
  • #2
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
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.
 

FAQ: Return vector from function (fortran 95)

What is a return vector in Fortran 95?

A return vector in Fortran 95 is a data structure that contains a set of values returned from a function. It is similar to a regular variable, but it can hold multiple values instead of just one.

How do I create a return vector in Fortran 95?

To create a return vector in Fortran 95, you need to declare it in the function's definition using the intent(out) attribute. This tells the compiler that the variable is intended to hold the return values from the function.

Can a return vector have different data types in Fortran 95?

Yes, a return vector in Fortran 95 can contain different data types. This is useful when a function needs to return multiple values of different types. However, the data types must be declared in the function's definition using the intent(out) attribute.

How do I access the values in a return vector in Fortran 95?

To access the values in a return vector in Fortran 95, you can use indexing or a loop. The values are stored in the vector in the same order as they are returned from the function. So, the first value can be accessed using vector(1), the second value using vector(2), and so on.

Can I pass a return vector as an argument to another function in Fortran 95?

Yes, you can pass a return vector as an argument to another function in Fortran 95. This allows you to use the values returned from one function as input for another function. However, the function that receives the return vector must have a compatible data type and shape for the argument.

Similar threads

Replies
5
Views
1K
Replies
7
Views
2K
Replies
13
Views
3K
Replies
3
Views
6K
Replies
2
Views
6K
Replies
5
Views
2K
Back
Top