# "Anonymous" functions

**URL:** <https://fortran-lang.discourse.group/t/anonymous-functions/8903>\
**Category:** Help\
**Created:** [November 29, 2024, 11:00am UTC](https://fortran-lang.discourse.group/t/anonymous-functions/8903 "2024-11-29T11:00:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cwetaski](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@cwetaski](https://fortran-lang.discourse.group/u/cwetaski)\
**Post date:** [November 29, 2024, 11:00am UTC](https://fortran-lang.discourse.group/t/anonymous-functions/8903/1 "2024-11-29T11:00:20Z")

</div>

Hello, I’m new to fortran and working with some others’ code. I noticed that the code basically stores anonymous functions in scalar variables, which when I see fortran is not supposed to support.

```auto
program mwe1
    DOUBLE PRECISION :: f,x
    f(x)= x**2 + 1.0d0

    print *, f(1.0d0),f(2.0d0),f(3.0d0)

end program

```

This code compiles (gfortran 14.2.0) without warning and outputs:  
`2.0000000000000000 5.0000000000000000 10.000000000000000`

Why does this code work,and what is happening? Is this bad practice or normal? I can’t seem to many other examples using this so I am assuming it’s bad practice.

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [November 29, 2024, 11:04am UTC](https://fortran-lang.discourse.group/t/anonymous-functions/8903/2 "2024-11-29T11:04:53Z")

</div>

You have a case of so-called statement functions. They are a rather old feature and actually error-prone and very limited. The modern way is to define internal functions:

```auto
program mwe1
    DOUBLE PRECISION :: f,x

    print *, f(1.0d0),f(2.0d0),f(3.0d0)
contains
    f(x)= x**2 + 1.0d0
end program

```

Such internal functions or subroutines are just as versatile as ordinary routines, but they are only seen within the containing routine or program.

Statement functions consist of a single statement (assignment and expression) only and can be placed in odd locations. Which makes them rather ambiguous when it comes to parsing the code.

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [November 29, 2024, 11:21am UTC](https://fortran-lang.discourse.group/t/anonymous-functions/8903/3 "2024-11-29T11:21:34Z")

</div>

> [@Arjen](#):
>
> ```auto
> contains
> f(x)= x**2 + 1.0d0
> 
> ```

Not standard Fortran, I am afraid. Standard Fortran expects an internal-subprogram after a CONTAINS, which is either a function-subprogram or subroutine-subprogram, not a stmt-function-stmt. The standard way would be:

```auto
contains
   double precision function f(y)
       double precision :: y
       f = y**2 + 1.0d0
   end function f

```

But the OP was correct, there are no anonymous functions in Fortran. In Fortran, all procedures are present at compilation, there is no creating subprograms during runtime (like BASIC’s EVAL).

---

<div class="post-metadata">

**Author:** ![cwetaski](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@cwetaski](https://fortran-lang.discourse.group/u/cwetaski)\
**Post date:** [November 29, 2024, 11:28am UTC](https://fortran-lang.discourse.group/t/anonymous-functions/8903/4 "2024-11-29T11:28:59Z")

</div>

Thank you both. This makes sense now. I will add “removing statement functions” to my refactoring to-do list.

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [November 29, 2024, 11:31am UTC](https://fortran-lang.discourse.group/t/anonymous-functions/8903/5 "2024-11-29T11:31:23Z")

</div>

Oh dear, my bad! That happens if you’re too hasty to answer and part of your brain is still busy with other stuff.
