[問題] function如何回傳陣列(矩陣)

看板Fortran作者 ( )時間12年前 (2012/06/01 00:14), 編輯推噓2(206)
留言8則, 4人參與, 最新討論串1/1
想使用function回傳一整個陣列而不是一個數值,不知到與法該怎麼用 program main real*8, allocatable :: a(:,:),a1(:,:)) integer*4 :: b=3 allocate (a(b,b),a1(b,b)) !allocate (a1(b,b)) allocate (a2(b,b)) a=0.d0; a1=0.d0; a1(1,1:3)=1.5d0; a(2,1:3)=2d0*a1(1,1:3)+5d0 write(*,*) a; write(*,*) a1; print * , '\' write(*,*) add(b,a,a1) pause stop end program main function add(b,a,a1) integer*4 :: b real*8,allocatable :: a(:,:),a1(:,:) allocate( a(b,b),a1(b,b) ) real*8, dimension(b,b) :: add add=a+a1 return end function add 跑出兩個錯誤訊息,不知道與法該怎麼用QQ Error: A specification statement cannot appear in the executable section. real*8, dimension(b,b) :: add --------^ Error: The shapes of the array expressions do not conform. add=a+a1 --------^ 除此之外,順便問一下可以自動加入行號嗎....難道真的要一行一行打QQ? 我用的是Compaq Visual Fortran 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.121.23.113

06/01 11:03, , 1F
第一個錯誤 請把宣告放在 allocate 前面
06/01 11:03, 1F

06/01 11:04, , 2F
第二個錯誤 屬於基本問題 請看有關陣列運算的章節
06/01 11:04, 2F

06/01 11:20, , 3F
第二個錯誤其實就是第一個引起的,解決第一個就可以了
06/01 11:20, 3F

06/01 11:21, , 4F
但是副程式要用allocatable的話要加interface,或者把它
06/01 11:21, 4F

06/01 11:21, , 5F
用contains包在主程式內
06/01 11:21, 5F

06/01 11:22, , 6F
對了,你這個程式裡還有另外的錯誤,但都容易解決
06/01 11:22, 6F

06/01 15:10, , 7F
所以要用到interface就是了?我再看看,感謝
06/01 15:10, 7F

06/12 03:52, , 8F
我的經驗是把這種函數寫在MODULE再USE該MODULE就行了
06/12 03:52, 8F
文章代碼(AID): #1FnvZx-B (Fortran)