# Initialization constant

**URL:** <https://fortran-lang.discourse.group/t/initialization-constant/3818>\
**Category:** Language enhancement\
**Created:** [June 22, 2022, 2:10pm UTC](https://fortran-lang.discourse.group/t/initialization-constant/3818 "2022-06-22T14:10:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![egio](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@egio](https://fortran-lang.discourse.group/u/egio)\
**Post date:** [June 22, 2022, 2:10pm UTC](https://fortran-lang.discourse.group/t/initialization-constant/3818/1 "2022-06-22T14:10:30Z")

</div>

I don’t know if it has been already proposed. Basically if I have a procedure that has an assumed shape array as an argument, I can define the size of an additional array using the information that I can get from the arguments of my procedure, like:

```auto
subroutine my_sub(a)
real :: a(:)
real :: c(size(a)*2 + 7*size(a)**2), d( 2*(size(a)*2 + 7*size(a)**2))
...

```

It would be nice to write something like:

```auto
subroutine my_sub(a)
real :: a(:)
integer, init_expression :: n = size(a)*2 + 7*size(a)**2
integer, init_expression :: m = 2*n
real :: c(n), d(m)

```

Where `n` and `m` will be like a constants inside the routine, while they will be different at each invocation of the procedure.

Obviously, the same can be easily obtained with allocatables, I know that.

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [June 23, 2022, 2:45am UTC](https://fortran-lang.discourse.group/t/initialization-constant/3818/2 "2022-06-23T02:45:30Z")

</div>

There have been several discussions about allowing for variables to be  
initialized in the declarations at the time of the call of the procedure,  
instead of the current situation where they are essentially initialized  
at program initialization and then SAVED (assuming they are variables  
and not parameter constants). But I do not remember anyone proposing  
anything exactly like this. People coming to Fortran with previous C experience in  
particular argue for something similiar where

```fortran
subroutine A(n)
integer :: i=10*n
end subroutine A

```

would set “i” on entry and allow for variables in the initialization, more like C; instead of acting more like a FORTRAN DATA statement and requiring constants.

Note that as well as an allocatable array (as you mentioned) you can pass  
the dimensions as arguments, call a routine that declares the values  
and then calls another routine (which could be a CONTAINED routine,  
or use BLOCK and ASSOCIATE. Not exactly the same, but somewhat in the  
same spirit. Might be others, but these are the closest standard methods  
that came to mind that are somewhat similar …

Using BLOCK …

```fortran
subroutine my_sub(a,w)
real,intent(in) :: a(:)
real,intent(out) :: w
integer :: m,n
   m=size(a)*2
   n=7*size(a)**2
   block
   real :: e(m), f(n)
      w = size(e)+size(f)
   end block

end subroutine my_sub

```

Using ASSOCIATE and BLOCK …

```fortran
subroutine my_sub(a,w)
real,intent(in) :: a(:)
real,intent(out) :: w
associate ( m=>size(a)*2, n=>7*size(a)**2)
block
   real :: e(m), f(n)
      w = size(e)+size(f)
end block
end associate
end subroutine my_sub

```

CONTAINED …

```fortran
module m_subs
contains 
   subroutine my_sub(a,w)
   implicit none
   real,intent(in) :: a(:)
   real,intent(out) :: w
   call real_my_sub( m = size(a)*2, n = 7*size(a)**2 )
   contains 
      subroutine real_my_sub(m,n)
      integer :: m,n
      real :: e(m), f(n)
            w = size(e)+size(f)
      end subroutine real_my_sub
   end subroutine my_sub

end module m_subs
program testit
use m_subs, only : my_sub
implicit none
real :: a(10)
real :: w

   call my_sub(a,w)
   write(*,*)w
end program testit

```

I would not rate any of those highly intuitive or succinct; but they do something similar.

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [June 23, 2022, 5:39am UTC](https://fortran-lang.discourse.group/t/initialization-constant/3818/3 "2022-06-23T05:39:58Z")

</div>

Here is another way to achieve the result.

```auto
subroutine my_sub(a,w)
#define m size(a)*2
#define n 7*size(a)**2
real,intent(in) :: a(:)
real,intent(out) :: w
real :: e(m), f(n)
w = size(e)+size(f)
end subroutine my_sub

```

I would not necessarily recommend this approach either, but it does work as expected. Of course, the preprocessor directives are not part of the fortran standard, but they are a decades-old de facto standard, and in my opinion should have been standardized in the formal sense some three decades ago. If you do something like this, at least change the macros from “m” and “n” to something else that stands out and is less likely to cause problems elsewhere in the file.

One other thing should be mentioned. As a practical matter, this is not exactly equivalent to declaring the arrays as allocatable and allocating them within the body of the subroutine. In this code, the arrays are automatic. Automatic arrays typically live on the stack, while allocatable arrays live in the heap. There are advantages and disadvantages to both approaches. In particular, if the arrays are small enough to not exceed the stack, then the automatic array approach is probably more efficient.

---

<div class="post-metadata">

**Author:** ![egio](https://avatars.discourse-cdn.com/v4/letter/e/dbc845/32.png) [@egio](https://fortran-lang.discourse.group/u/egio)\
**Post date:** [June 23, 2022, 9:17am UTC](https://fortran-lang.discourse.group/t/initialization-constant/3818/4 "2022-06-23T09:17:44Z")

</div>

I haven’t thought at the block construct.  
Nice idea!
