In Fortran, is it possible to define a function which returns multiple values like below?
[a, b] = myfunc(x, y)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With