W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
a(1)
修改為 a(3)
并保持?jǐn)?shù)組 a 的長(zhǎng)度為 10,即數(shù)組 a 和數(shù)組 x 不從第一個(gè)元素開(kāi)始對(duì)應(yīng),那么源代碼將無(wú)法被編譯,如示例 5 的變種一所示。要想在這種情況下還能正常編譯,我們就必須擴(kuò)充數(shù)組 a 的長(zhǎng)度至少為 12。這樣一來(lái),子程序中要賦值的數(shù)都能正確存入數(shù)組 x 并返回給主程序的數(shù)組變量 a,如示例 5 的變種二所示。!!! 示例 5 的變種一
program stest5
implicit none
real a(10)
call sub(a(3))
print *, a(3)
end program stest5
subroutine sub(x)
implicit none
real x(10)
integer i
do i = 1, 10
x(i) = i
enddo
end subroutine sub
!!! 執(zhí)行結(jié)果
>>> Error: Actual argument contains too few elements for dummy argument 'x' (8/10) at (1)
!!! 示例 5 的變種二
program stest5
implicit none
real a(12)
call sub(a(3))
print *, a(3)
end program stest5
subroutine sub(x)
implicit none
real x(10)
integer i
do i = 1, 10
x(i) = i
enddo
end subroutine sub
!!! 執(zhí)行結(jié)果
>>> 1.00000000
其實(shí),在子程序中也可以不定義數(shù)組的長(zhǎng)度,將長(zhǎng)度設(shè)置為 ?*
?。
如下示例 6 所示,子程序的功能是將數(shù)組 a 中的前 n 個(gè)元素復(fù)制給數(shù)組 b。
!!! 示例 6
subroutine copy(a, b, n)
implicit none
real a(*), b(*)
integer n, i
do i = i, n
b(i) = a(i)
enddo
end subroutine copy
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: