Capítulo 8. Funções e Subrotinas - UFMG
Capítulo 8. Funções e Subrotinas - UFMG
Capítulo 8. Funções e Subrotinas - UFMG
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
102 <strong>8.</strong>2. sub-programas<br />
! Gera uma matriz de pontos de uma função q u a l q u e r d e s t i n a d a à plotagem<br />
! do g r á f i c o d e s t a função .<br />
! Parâmetros :<br />
! f : Função externa a s e r p l o t a d a .<br />
! x i : Ponto i n i c i a l ( entrada ) .<br />
! x f : Ponto f i n a l ( entrada ) .<br />
! npt : Número de pontos a s e r gerados ( entrada )<br />
! p l o t : matriz de forma (/ npt , 2 /) contendo as a b c i s s a s e ordenadas dos<br />
! pontos ( saída ) .<br />
!<br />
subroutine p l o t a ( f , xi , xf , npt , p l o t )<br />
implicit none<br />
integer , intent ( in ) : : npt<br />
real , intent ( in ) : : xi , xf<br />
real , dimension ( 2 , npt ) , intent ( out ) : : p l o t<br />
INTERFACE<br />
function f ( x )<br />
real : : f<br />
real , intent ( in ) : : x<br />
end function f<br />
END INTERFACE<br />
! V a r i á v e i s l o c a i s :<br />
integer : : i<br />
real : : passo , x<br />
!<br />
passo= ( xf − x i )/ real ( npt − 1)<br />
x= x i<br />
do i= 1 , npt<br />
p l o t ( 1 , i )= x<br />
p l o t ( 2 , i )= f ( x )<br />
x= x + passo<br />
end do<br />
return<br />
end subroutine p l o t a<br />
Considere o seguinte exemplo de uma função de valor matricial:<br />
PROGRAM TES_VAL_MAT<br />
IMPLICIT NONE<br />
INTEGER, PARAMETER :: M= 6<br />
INTEGER, DIMENSION(M,M) :: IM1, IM2<br />
...<br />
IM2= FUN_VAL_MAT(IM1,1) !Chama funç~ao matricial.<br />
...<br />
CONTAINS<br />
FUNCTION FUN_VAL_MAT(IMA, SCAL)<br />
INTEGER, DIMENSION(:,:), INTENT(IN) :: IMA<br />
INTEGER, INTENT(IN) :: SCAL<br />
INTEGER, DIMENSION(SIZE(IMA,1),SIZE(IMA,2)) :: FUN_VAL_MAT<br />
FUN_VAL_MAT= IMA*SCAL<br />
END FUNCTION FUN_VAL_MAT<br />
END PROGRAM TES_VAL_MAT<br />
Neste exemplo, o limites das dimensões de FUN_VAL_MAT são herdadas do argumento verdadeiro transferido à<br />
função e usadas para determinar o tamanho da matriz resultante.<br />
Para que o compilador conheça a forma da matriz resultante, a interface de uma função de valor matricial<br />
deve ser explícita em todas as unidades onde esta função é invocada.<br />
Autor: Rudi Gaelzer – IFM/UFPel Impresso: 23 de abril de 2008