! Hauptprogramm ========================================================
program proceduren
implicit none

integer:: fakultaet, fibonacci, i
real:: arith_m, geom_m

interface
subroutine mittel(a,b,c, am, gm, mo, me)
real, intent(in):: a,b,c
real, intent(out), optional:: am, gm, mo, me
end subroutine mittel
end interface

write(*,*) fakultaet(5)                            ! 1. Bsp.
write(*,*) fibonacci(5)                            ! 2. Bsp.
do i=1,10                                          !
   write(*,"(X3I)") i, fakultaet(i), fibonacci(i)  ! Uebung
   end do                                          ! 

call mittel(2.0,4.0,8.0, arith_m, geom_m)          ! 3. Bsp.
write(*,*) arith_m, geom_m
call mittel(9.0,4.0,68.0, gm=geom_m)               ! 4. Bsp.
write(*,*) geom_m
end program proceduren

! Funktionen und Subroutinen ===========================================
integer function fakultaet(n)
integer :: i,f,n
f=1
do i=1,n
   f=f*i
   end do
fakultaet=f
end function fakultaet
!-----------------------------------------------------------------------
recursive integer function fibonacci(n) result(f)
implicit none
integer::n
select case (n) 
   case(0:1) 
      f=1
   case default 
      f=fibonacci(n-1)+fibonacci(n-2)
   end select
end function fibonacci
!-----------------------------------------------------------------------
subroutine mittel(a,b,c, am, gm, mo, me)
real, intent(in):: a,b,c
real, intent(out), optional:: am, gm, mo, me

if (present(am)) am=(a+b+c)/3.0
if (present(gm)) gm=(a*b*c)**(1./3.)
! ...
end subroutine mittel
