[fortran] как передать массив в подпрограмму
call ResetCells(xcur,ycur,zcur,vc,fc)
![](/images/graemlins/smile.gif)
выложи плиз полный текст программы
все зависит от того, какой у тя массив статический или разещаемый(динамический)
К Компаковскому и Интеловскому фортранам весьма приличный хелп прилагается и примеры там есть.
Вообще должна работать передача массива, как обычной переменной.
Но опять же смотря какая у тебя версия фортрана и компилятор...
От этого как раз не зависит.
real :: a(10)
...
subroutine test(b,ndim)
integer :: ndim
real :: b(ndim)
...
end subroutine test
...
call subroutine(a,10)
...
Это для произвольного стандарта.
P.S. common'ы использовать - признак дурного тона =). Типа, устаревшая конструкция.
Разве что в очень старых версиях.
real :: a(10)
...
subroutine test(b)
real :: b(*)
integer :: ndim=size(b) ! На случай, если размерность массива в подпрограмме нужна
...
end subroutine test
...
call subroutine(a)
...
В общем случае, твой пример не сработает.
Danger of calling Fortran 90 style routinesЭто отсюда из раздела про фортрановские "баги"
program main
real, dimension(5) :: x
x = 0.
! THIS IS WRONG
call incb(x)
print *, x
end program main
subroutine incb(a)
! this is a fortran90 style subroutine
real, dimension(:) :: a
a = a + 1.
end subroutine incb
Explanation
The subroutine incb uses a Fortran 90 style assumed shape array (containing dimension(.
Such routines must either be in a module, or have an explicit interface wherever they are used.
In this example, neither one was true.
One correct way to call such procedures is to use an explicit interface as follows:
program main
real, dimension(5) :: x
! THIS IS THE RIGHT WAY
interface
subroutine incb(a)
real, dimension(:) :: a
end subroutine incb
end interface
x = 0.
call incb(x)
print *, x
end program main
subroutine incb(a)
! this is a fortran90 style subroutine
real, dimension(:) :: a
a = a + 1.
end subroutine incb
If the routine is in a module interfaces are generated automatically and do not need to be explicitly written.
! THIS IS ANOTHER RIGHT WAY
module inc
contains
subroutine incb(a)
! this is a fortran90 style subroutine
real, dimension(:) :: a
a = a + 1.
end subroutine incb
end module inc
program main
use inc
real, dimension(5) :: x
x = 0.
call incb(x)
print *, x
end program main
If interfaces are used, the interface MUST match the actual function.
Оставить комментарий
pamsha
в subroutine и обратно?