CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c...
Transcript of CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c...
![Page 1: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/1.jpg)
CLASE 14
Computación 2018– FCAG-UNLP – Paula Benaglia
![Page 2: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/2.jpg)
Funciones externas Se utilizan cuando, para generar el valor deseado, hacen falta cálculos que involucran más de una sentencia. Ejemplo: -5, x < 0
f(x) = 0, x = 0 +5, x > 0
…... If (x.lt.0) then fx = -5. elseif(x.gt.0) then fx = 5. else fx = 0. endif
fx = 0. If (x.lt.0) then fx = -5. elseif(x.gt.0) then fx = 5. endif
5
-5
![Page 3: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/3.jpg)
Si se precisa usar nuevamente, o en otro programa, conviene armar una función externa o FUNCTION:
function fx(x) If (x.lt.0) then
fx = -5. elseif(x.gt.0) then fx = 5. else fx = 0.
endif return end
Desde el principal, se llama: program uno ….... q = fx (x) * z w = fx (y) + 4. …..... end
![Page 4: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/4.jpg)
La sentencia function indica que a partir de allí se va a definir una función externa. Estas líneas de código pueden estar a continuación del programa principal – en el mismo archivo- o en un archivo aparte. (1) (2)
![Page 5: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/5.jpg)
Ventajas de cada forma … Para compilar (1): gfortran –o progr1 progr1.f Para compilar (2): se compila primero la function, y luego el programa principal, seguido por la function:
gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o
Las variables son locales a la propia function: las variables usadas en la function no se relacionan con otras de igual nombre en el programa principal.
Cuando dentro de la function se le asigna valor a la misma, ésta va sin argumentos.
![Page 6: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/6.jpg)
n n n! k k (n-k)! k!
( x + y ) n = Σ yn-k xk
Sea b(k) = xk yn-k, =
n, k son enteros positivos; n ≥ k
Problema: calcular un binomio a la n n n k k=0
Coeficiente binomial
![Page 7: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/7.jpg)
Calcular (x+y)n
program binnew write(*,*) ‘Ingrese x, y, n:’ read (*,*) x, y, n sumat = 0. do k = 0, n coefic = ifact(n) / ifact(k) / ifact (n-k) sumat = sumat + coefic * x**(n-k) * y**k enddo write(*,*) ‘ (‘, x, ’+’, y,’)^’, n, ‘=‘ , sumat end
Integer function ifact(m) Integer iprod, i, m iprod = 1 do i = 1, m iprod = iprod * m Enddo ifact = iprod return end
![Page 8: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/8.jpg)
Arreglos ajustables Cuando la longitud del arreglo a transmitir es variable de acuerdo al momento en que se evalúe la función, podemos mandar como argumento las dimensiones del arreglo a utilizar en la Function:
function opvect (n, flujos) integer flujos(n) …. ....
Se puede pasar así a un subprograma parte de un arreglo dimensionado en el programa principal (desde un elemento, hasta todos). No se puede dimensionar el arreglo con más elementos que los que tiene en el programa principal.
![Page 9: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/9.jpg)
Ejercicio: subprograma para calcular la cantidad de vocales de una palabra
Integer function vocales(palabra) character*(*) palabra vocales = 0 do k=1, len(palabra) if ( index(‘aeiou’, palabra(k:k)) .ne. 0) then
vocales = vocales + 1 endif enddo end
![Page 10: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/10.jpg)
![Page 11: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/11.jpg)
![Page 12: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/12.jpg)
![Page 13: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/13.jpg)
![Page 14: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/14.jpg)
![Page 15: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/15.jpg)
convrt
![Page 16: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/16.jpg)
Luego de la llamada a la subrutina, N cambió de valor ….
![Page 17: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/17.jpg)
El programa funciona igual que el anterior. Si bien i tiene el valor de la constante 5, a ella no se le cambia su valor en la subrutina, por lo que la sintaxis es válida.
![Page 18: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/18.jpg)
J = i + j end
La salida del proceso es dependiente del sistema: puede haber mensaje de error (por querer sobreescribir el valor de la variable ‘20’), o puede haber sobreescritura del valor 25 sobre el valor 20 para la variable J…
![Page 19: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/19.jpg)
![Page 20: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/20.jpg)
![Page 21: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/21.jpg)
![Page 22: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/22.jpg)
Declaración con dimensión ajustable
![Page 23: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/23.jpg)
![Page 24: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/24.jpg)
Mas subrutinas … • De suma de matrices
• De trasposicion
• De producto
• Etc …
![Page 25: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/25.jpg)
Mas subrutinas … • De suma de matrices
• De trasposicion
• De producto
• Etc …
![Page 26: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/26.jpg)
Comprobar la identidad entre matrices: ( A + B) . C = A . C + B . C
![Page 27: CLASE 14 - UNLPvalhalla.fcaglp.unlp.edu.ar/computacion/Teorias-2018-SS/Clase-14... · gfortran –c fun1.f à fun1.o gfortran –o progr2 progr2.f fun1.o Las variables son locales](https://reader036.fdocuments.net/reader036/viewer/2022071002/5fbf81c6011d90525052613e/html5/thumbnails/27.jpg)
FIN CLASE 14