# Function not working

**URL:** <https://fortran-lang.discourse.group/t/function-not-working/3745>\
**Category:** Help\
**Created:** [June 13, 2022, 8:53pm UTC](https://fortran-lang.discourse.group/t/function-not-working/3745 "2022-06-13T20:53:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rakanott](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@rakanott](https://fortran-lang.discourse.group/u/rakanott)\
**Post date:** [June 13, 2022, 8:53pm UTC](https://fortran-lang.discourse.group/t/function-not-working/3745/1 "2022-06-13T20:53:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [June 13, 2022, 9:02pm UTC](https://fortran-lang.discourse.group/t/function-not-working/3745/2 "2022-06-13T21:02:40Z")

</div>

Welcome to the forum. You can declare the type of func in the main program

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![rakanott](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@rakanott](https://fortran-lang.discourse.group/u/rakanott)\
**Post date:** [June 14, 2022, 6:52pm UTC](https://fortran-lang.discourse.group/t/function-not-working/3745/3 "2022-06-14T18:52:06Z")

</div>

Thanks a lot, these Fortran functions definatly differ from functions in other languages!
