# How does the compiler handle functions and subroutines?

**URL:** <https://fortran-lang.discourse.group/t/how-does-the-compiler-handle-functions-and-subroutines/4941>\
**Category:** Help\
**Created:** [December 24, 2022, 8:20pm UTC](https://fortran-lang.discourse.group/t/how-does-the-compiler-handle-functions-and-subroutines/4941 "2022-12-24T20:20:04Z")\
**Posts on this page:** 3\
**Page:** 2

<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:** [February 15, 2023, 7:41pm UTC](https://fortran-lang.discourse.group/t/how-does-the-compiler-handle-functions-and-subroutines/4941/23 "2023-02-15T19:41:54Z")

</div>

The code can also be factored using `associate`. Here is an example:

```auto
module m
   implicit none
   integer, parameter :: N = 2
   real :: x(N,N)
end module
program p
   use m, only : x
   implicit none
   x = reshape( [1.0, 2.0, 3.0, 4.0], shape=shape(x) )
   associate( a=>x(:,1), b=>x(:,2) )
      print *, "X = ", x
      print *, "A = ", a
      print *, "B = ", b
   end associate
end program p

```

I have no idea how or if this kind of thing could be automated.

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [February 15, 2023, 9:35pm UTC](https://fortran-lang.discourse.group/t/how-does-the-compiler-handle-functions-and-subroutines/4941/24 "2023-02-15T21:35:21Z")

</div>

> [@RonShepard](#):
>
> The code can also be factored using `associate`

With the exercise, the expectation is a subprogram `sub` shall remain after refactoring. That presently the legacy sub makes use of some global.data `x` that is currently in a `COMMON` block, the data are initialized elsewhere (main program), and the data are referenced via `EQUIVALENCE`d objects of “a” and “b”. Such practice wasn’t uncommon with legacy codes with FORTRAN 77 and dialects and extensions. The print statements in the small example simply illustrated such use of the data in a couple of lines but the actual code may have further instructions taking place on the data. Getting rid of the subprogram altogether then is not what is called for.

---

<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:** [February 15, 2023, 9:59pm UTC](https://fortran-lang.discourse.group/t/how-does-the-compiler-handle-functions-and-subroutines/4941/25 "2023-02-15T21:59:19Z")

</div>

> [@FortranFan](#):
>
> The print statements in the small example simply illustrated such use of the data in a couple of lines but the actual code may have further instructions taking place on the data. Getting rid of the subprogram altogether then is not what is called for.

I agree with all that, I was just trying to show that instead of explicitly declared pointers, one might consider using the new `associate` construct instead. The real issue is whether any of these things can be done automatically.

[Previous page](https://fortran-lang.discourse.group/t/how-does-the-compiler-handle-functions-and-subroutines/4941.md?page=1)
