Skip to content
Snippets Groups Projects
Commit 8bd73c76 authored by Jonas Riedel's avatar Jonas Riedel
Browse files

Upload New File

parent 7d8d1bc1
No related branches found
No related tags found
No related merge requests found
module function_introduction_module
implicit none
private !Modulinformationen privat
public :: f, ggT !Export der Funktionen
contains
function f(x) !Funktionsname und formale Argumente
integer, intent(in) :: x !Funktionsparameterdeklarierung (Typen), intent()-Attribute
integer :: f !Funktionswertdeklaierung, kein intent()-Attribut
real :: grenze !lokale Variablendeklaration, Zugriff nur innerhalb der Funktion
grenze = 1.5
if(x > grenze)then
f = x*x
else
f = x
end if
end function f
recursive function ggT(a,b) result(gcd)
integer, intent(in) :: a,b
integer :: gcd
if(b == 0) then !Basisfall
gcd = a
else
gcd = ggT(b, modulo(a,b)) !rekursiver Fall
end if
end function
! Schreibe eine Funktion welche eine Zahl n als Parameter nutzt um die Summe der ersten n natürlichen Zahlen zu berechnen.
! Schreibe eine rekursive Funktion, welche eine Zahl n als Parameter nutzt um die Summe der ersten n natürlichen Zahlen zu berechnen.
! Schreibe eine Funktion welche eine Zahl n als Parameter nutzt und überprüft, ob diese eine perfekte Zahl ist. (n == Summe echter Teiler)
end module function_introduction_module
program function_introduction
use function_introduction_module
implicit none
integer :: x
x = 2
write(*,*)"This is the result of a function f applied on x (in this case 2):"
write(*,*) f(x) !Funktionsaufruf
write(*,*)"This is the result of a function ggT applied on 5 and 10:"
write(*,*) ggT(10,12) !Funktionsaufruf
end program function_introduction
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment