solalito
solalito

Reputation: 1209

Call array functions in Fortran

I want to assign the return values of an array functions of size N to N scalars. In the simplest case, the array is of size 2 like shown below:

PROGRAM ARRAY_FUNCTIONS
IMPLICIT NONE
REAL :: x_input , y_input
REAL :: x_output, y_output
REAL, DIMENSION(0:1) :: a_dummy

x_input = 1.0
y_input = 2.0

a_dummy = Test_Array(x_input, y_input)

x_output = a_dummy(0)
y_output = a_dummy(1)    

CONTAINS
  FUNCTION Test_Array(x1,y1)
  REAL, DIMENSION(0:1) :: Test_Array

  Test_Array(0) = 2*x1
  Test_Array(1) = 2*x1
  END FUNCTION Test_Array
END PROGRAM ARRAY_FUNCTIONS

Is there a way around declaring a dummy variable "a_dummy" and assigning my scalars "x_input" and "y_input" through that variable?

Upvotes: 1

Views: 129

Answers (1)

Alexander Vogt
Alexander Vogt

Reputation: 18098

As far as I know, it is not possible without at least some declaration of a temporary array. You could try pointers to save some memory, though:

!...
REAL, POINTER :: x_output, y_output
REAL, DIMENSION(0:1),TARGET :: a_dummy

x_input = 1.0
y_input = 2.0

a_dummy = Test_Array(x_input, y_input)

x_output => a_dummy(0)
y_output => a_dummy(1) 
!... 

Why don't you work on input and output arrays directly?

As for x_input and y_input, you can specify the values directly in the function call:

!...
a_dummy = Test_Array(1.0, 2.0)
!...

Upvotes: 1

Related Questions