# Subroutines that takes array of variable rank as input

**URL:** <https://fortran-lang.discourse.group/t/subroutines-that-takes-array-of-variable-rank-as-input/450>\
**Category:** Help\
**Created:** [November 30, 2020, 11:41pm UTC](https://fortran-lang.discourse.group/t/subroutines-that-takes-array-of-variable-rank-as-input/450 "2020-11-30T23:41:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niko](https://avatars.discourse-cdn.com/v4/letter/n/91b2a8/32.png) [@Niko](https://fortran-lang.discourse.group/u/Niko)\
**Post date:** [November 30, 2020, 11:41pm UTC](https://fortran-lang.discourse.group/t/subroutines-that-takes-array-of-variable-rank-as-input/450/1 "2020-11-30T23:41:54Z")

</div>

Dear all,

I’m working on a function that should ideally work for an input array that may have, in different calls, rank 0, 1, 2 or 3.

SELECT RANK seems like doing the job, but as it’s a new feature and the rest of the code is mostly F90 I hesitate a bit to introduce it.

Alternatively, I can always declare a 3D arrays and then keep number of elements in one or two or all three dimensions equal to 1. However, than the code starts to look really ugly (it’s a long code with dozen of variables like that) and I’d rather have some more elegant solution.

What do you do in such situation? Any recommendations? Any example?

Many thanks!  
Niko

---

<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:** [December 1, 2020, 12:18am UTC](https://fortran-lang.discourse.group/t/subroutines-that-takes-array-of-variable-rank-as-input/450/2 "2020-12-01T00:18:10Z")

</div>

> [@Niko](#):
>
> What do you do in such situation? Any recommendations? Any example?

@Niko,

See my comments at a similar question at another [**forum**](https://community.intel.com/t5/Intel-Fortran-Compiler/fortran-allocation-unknown-a-priori-DIMENSION/m-p/1228732/highlight/true#M152672) .

And an example at this [**link**](https://community.intel.com/t5/Intel-Fortran-Compiler/fortran-allocation-unknown-a-priori-DIMENSION/m-p/1228868/highlight/true#M152700).

So one option you can consider is to take advantage of the Fortran facility where the target of a RANK-N array pointer can be a RANK-1 array. This can greatly simplify the code structure depending on your needs.

That is, elevate your code design a step higher so that the circumstances where you get into situations where you require subroutines with dummy arguments of different ranks are greatly diminished or eliminated altogether.

---

<div class="post-metadata">

**Author:** ![Niko](https://avatars.discourse-cdn.com/v4/letter/n/91b2a8/32.png) [@Niko](https://fortran-lang.discourse.group/u/Niko)\
**Post date:** [December 1, 2020, 9:56am UTC](https://fortran-lang.discourse.group/t/subroutines-that-takes-array-of-variable-rank-as-input/450/3 "2020-12-01T09:56:24Z")

</div>

Nice. That’s a pretty neat way of doing it! Many thanks!
