Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function which returns multiple values

In Fortran, is it possible to define a function which returns multiple values like below?

[a, b] = myfunc(x, y)
like image 627
Harutaka Kawamura Avatar asked May 09 '16 15:05

Harutaka Kawamura


1 Answers

That depends... With functions, it is not possible to have two distinct function results. However, you can have an array of length two returned from the function.

  function myfunc(x, y)
    implicit none
    integer, intent(in) :: x,y
    integer             :: myfunc(2)

    myfunc = [ 2*x, 3*y ]
  end function

If you need two return values to two distinct variables, use a subroutine instead:

  subroutine myfunc(x, y, a, b)
    implicit none
    integer, intent(in) :: x,y
    integer, intent(out):: a,b

    a = 2*x
    b = 3*y
  end subroutine
like image 197
Alexander Vogt Avatar answered Oct 02 '22 01:10

Alexander Vogt