Hello! I’m a complete newbie, and fortran is very obscure to me, i can’t seem to figure out why this code generates errors. Could soneone please help me? That would be much appreciated.
function func(arg) result(retval)
integer, intent(in) :: arg
integer :: retval
end function func
program functions
implicit none
integer:: randomint
randomint = func(5)
end program functions
2 Likes
Welcome to the forum. You can declare the type of func in the main program
function func(arg) result(retval)
integer, intent(in) :: arg
integer :: retval
retval = 2*arg
end function func
!
program functions
implicit none
integer :: func, randomint
randomint = func(5)
print*,randomint ! 10
end program functions
or you could put the function in a module and use it in the main program, which is what I recommend:
module m
implicit none
contains
function func(arg) result(retval)
integer, intent(in) :: arg
integer :: retval
retval = 2*arg
end function func
end module m
!
program functions
use m
implicit none
integer :: randomint
randomint = func(5)
print*,randomint ! 10
end program functions
A function should set its result, as shown.
3 Likes
Thanks a lot, these Fortran functions definatly differ from functions in other languages!