[fortran] как передать массив в подпрограмму

pamsha

в subroutine и обратно
?

oleg_n

common /curcel/fc(nctot)
call ResetCells(xcur,ycur,zcur,vc,fc)

pamsha

привет
выложи плиз полный текст программы

-Serg-

все зависит от того, какой у тя массив статический или разещаемый(динамический)

durka82

А на чем пишешь?
К Компаковскому и Интеловскому фортранам весьма приличный хелп прилагается и примеры там есть.
Вообще должна работать передача массива, как обычной переменной.
Но опять же смотря какая у тебя версия фортрана и компилятор...

durka82

От этого как раз не зависит.

lili197602


real :: a(10)
...
subroutine test(b,ndim)
integer :: ndim
real :: b(ndim)
...
end subroutine test
...
call subroutine(a,10)
...

Это для произвольного стандарта.
P.S. common'ы использовать - признак дурного тона =). Типа, устаревшая конструкция.

durka82

Вообще размерность массива передавать необязательно.
Разве что в очень старых версиях.
real :: a(10)
...
subroutine test(b)
real :: b(*)
integer :: ndim=size(b) ! На случай, если размерность массива в подпрограмме нужна
...
end subroutine test
...
call subroutine(a)
...

lili197602

Версии тут не причем.
В общем случае, твой пример не сработает.
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.
Это отсюда из раздела про фортрановские "баги"
Оставить комментарий
Имя или ник:
Комментарий: