1

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?

0

1 Answer 1

1

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)
!...
Sign up to request clarification or add additional context in comments.

1 Comment

I simplified what I actually want to do. In my code, x_input and y_input are actually 3 dimensional arrays (function of x, y and time) so I don't really want to add another dimension to them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.