25.06.2013 Views

Il Linguaggio Fortran 90/95

Il Linguaggio Fortran 90/95

Il Linguaggio Fortran 90/95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5.9 Funzioni array 259<br />

PROGRAM test_array_func<br />

IMPLICIT NONE<br />

INTEGER, PARAMETER :: m=6<br />

INTEGER, DIMENSION (m,m) :: im1, im2<br />

... ! assegnazione di m<br />

im2 = array_func(im1,1)<br />

...<br />

CONTAINS<br />

FUNCTION array_func(x,cost)<br />

INTEGER, INTENT(IN) :: x(:,:)<br />

INTEGER, INTENT(IN) :: cost<br />

INTEGER :: array_func(SIZE(x,1),SIZE(x,2))<br />

array_func(:,:) = cost*x(:,:)<br />

END FUNCTION array_func<br />

END PROGRAM test_array_func<br />

Certamente in questi esempi si sarebbe potuto dichiarare il risultato anche con dimensioni fisse<br />

(ossia come un explicit shape array) ma questo approccio sarebbe stato molto meno flessibile.<br />

Si noti che per default le funzioni sono sempre pensate produrre un risultato scalare per<br />

cui in tutti gli altri casi deve essere fornita una interfaccia esplicita nell’unità di programma<br />

chiamante. Questo naturalmente è vero non soltanto per le funzioni che restituiscono un array<br />

ma anche per tutte quelle funzioni che restituiscono puntatori oppure oggetti di tipo derivato.<br />

Mentre per le procedure interne o di modulo non sussiste alcun problema, per le procedure<br />

esterne deve essere previsto un opportuno interface block. Di seguito viene riportato lo stesso<br />

esempio di prima, questa volta però con l’ausilio di una funzione esterna:<br />

PROGRAM test_array_func<br />

IMPLICIT NONE<br />

INTERFACE ! obbligatorio<br />

FUNCTION array_func(ima,scal)<br />

INTEGER, INTENT(IN) :: ima(:,:)<br />

INTEGER, INTENT(IN) :: scal<br />

INTEGER, DIMENSION(SIZE(ima,1),SIZE(ima,2)) :: array_func<br />

END FUNCTION array_func<br />

END INTERFACE<br />

INTEGER, PARAMETER :: m = 6<br />

INTEGER, DIMENSION(m,m) :: im1, im2<br />

...<br />

im2 = array_func(im1,1)<br />

...<br />

END PROGRAM test_array_func<br />

FUNCTION array_func(x,cost)<br />

IMPLICIT NONE

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!