Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

how to pass array into subroutine?

Byoungho_Park
Beginner
718 Views
Hello

I'm trying to pass the array into subroutine.

Please look at my first program.

program t1

implicit none

integer, dimension(8) :: myarr

myarr = (/1,2,3,4,5,6,7,8/)

call passarr(myarr(5), 4)

contains

subroutine passarr(a,n)

integer, intent(in) :: N

integer, dimension(N), intent(in out) :: A

integer :: i

do i = 1, n

write(*,*) A(i)

end do

end subroutine passarr

end program t1

The results are
5
6
7
8

I want to implement this code for 2 dimensional arrary.

Please look at my second program.

program t2

implicit none

integer, dimension(8,2) :: myarr

integer :: i

myarr = reshape((/1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16/),(/8,2/))

call passarr(myarr(5,:), 4)

contains

subroutine passarr(a,n)

integer, intent(in) :: N

integer, dimension(N,2), intent(in out) :: A

write(*,*) ''

do i = 1, n

write(*,*) A(i,:)

end do

end subroutine passarr

end program t2

I hope that the results are
5 13
6 14
7 15
8 16.

However, the result are

5 3616201

13 0

2052332597 2136218672

134253992 201362856.

what is wrong?

Thanks in advance to your advice.

Park

0 Kudos
1 Solution
mecej4
Honored Contributor III
718 Views
In your program T2, you are not passing the intended array section properly. Replace

call passarr(myarr(5,:), 4)

by

call passarr(myarr(5:,:), 4)

View solution in original post

0 Kudos
1 Reply
mecej4
Honored Contributor III
719 Views
In your program T2, you are not passing the intended array section properly. Replace

call passarr(myarr(5,:), 4)

by

call passarr(myarr(5:,:), 4)
0 Kudos
Reply