# TRIVIAL: Using a REAL as mathematical function in the main program

**URL:** <https://fortran-lang.discourse.group/t/trivial-using-a-real-as-mathematical-function-in-the-main-program/5889>\
**Category:** Help\
**Created:** [May 31, 2023, 2:07pm UTC](https://fortran-lang.discourse.group/t/trivial-using-a-real-as-mathematical-function-in-the-main-program/5889 "2023-05-31T14:07:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ComicalIntersection](https://avatars.discourse-cdn.com/v4/letter/c/858c86/32.png) [@ComicalIntersection](https://fortran-lang.discourse.group/u/ComicalIntersection)\
**Post date:** [May 31, 2023, 2:07pm UTC](https://fortran-lang.discourse.group/t/trivial-using-a-real-as-mathematical-function-in-the-main-program/5889/1 "2023-05-31T14:07:06Z")

</div>

Good day,  
When reading old FORTRAN code I often encounter code like this:

When declaring variables:

```auto
PROGRAM ENTROPYINCREASER3000
IMPLICIT NONE
REAL :: FUNCTION, X, A, B, TRESH
REAL, ALLOCATABLE :: C(:), VALUE(:)
INTEGER :: STEP

```

which is later used in the code as follows (spoof example):

```auto
FUNCTION(X) = A*A*EXP(X)*SIN(X)/X
....
DO I=1,C_L
30 CONTINUE
      STEP = STEP +1 
      X = B+FUNCTION(X)*C(I)
      VALUE(I) = X*B
      IF (VALUE(I) .GT. TRESH) GOTO 30
   WRITE(35,50) C(I), VALUE(I), STEP
   STEP = 0
   X= 2.
END DO
50 FORMAT(2X, 'C-VAL=',E15.8,4X, 'VALUE=' ,E15.8,4X,'STEP=',I8)
....
END PROGRAM ENTROPYINCREASER3000 

```

…Which is often found inside a loop for some recursive task (i.e. finding roots) (also: the dots signify other unrelated code for example some if statement wether)

I would not expect this to work because as far as I know functions need to be declared separately behind some CONTAINS statement. Indeed when I try this inside a program I get errors of the form:

> FUNCTION(X) = A\*EXP(X)  
> 1  
> Error: Unclassifiable statement at (1)

Yet… when I isolate such a snippet and:  
… assign other parameters (like A) AFTER the FUNCTION(X) assignment  
… make sure that `FUNCTION(X)=...` is the first line of the program

if all are met then it works perfectly.

TL;DR Why does it even work in the first place? And why does assigning a value to A before you use it in the FUNCTION(X)=… make it unclassifiable? If no value for A is available, does FORTRAN wait in a Lambda-function-esque way which forces it to work once A is declared or is it far more obvious? I can not seem to find anything online but most likely I am unaware of how some things are named and this is extremely trivial, in which case I apologize.

---

<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:** [May 31, 2023, 2:18pm UTC](https://fortran-lang.discourse.group/t/trivial-using-a-real-as-mathematical-function-in-the-main-program/5889/2 "2023-05-31T14:18:43Z")

</div>

I think you are coming across [statement functions](https://www.ibm.com/docs/en/openxl-fortran-aix/17.1.0?topic=attributes-statement-function), which have a different syntax than regular functions.

---

<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:** [May 31, 2023, 2:19pm UTC](https://fortran-lang.discourse.group/t/trivial-using-a-real-as-mathematical-function-in-the-main-program/5889/3 "2023-05-31T14:19:02Z")

</div>

What you have encountered is a so-called statement function. It is superseded by internal routines or perhaps better: internal routines are a much better behaved feature that can be used to achieve the same thing. Try and find descriptions in old FORTRAN manuals.

---

<div class="post-metadata">

**Author:** ![ComicalIntersection](https://avatars.discourse-cdn.com/v4/letter/c/858c86/32.png) [@ComicalIntersection](https://fortran-lang.discourse.group/u/ComicalIntersection)\
**Post date:** [May 31, 2023, 2:35pm UTC](https://fortran-lang.discourse.group/t/trivial-using-a-real-as-mathematical-function-in-the-main-program/5889/4 "2023-05-31T14:35:33Z")

</div>

Ah thank you all, it has a name! (Both of you solved my problem but I can only select one).
