# When is "n" in explicit-shape array A(n) implicitly typed

**URL:** <https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389>\
**Category:** Help\
**Created:** [February 11, 2024, 4:41am UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389 "2024-02-11T04:41:24Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [February 11, 2024, 4:41am UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/1 "2024-02-11T04:41:24Z")

</div>

When you declare an explicit-shape array with the dimension declared after the array:

```auto
integer, intent(in) :: A(n)
integer, intent(in) :: n

```

Is the `n` in `A(n)` implicitly typed?

Here is a full test program:

```auto
program expr2
implicit none
integer :: y(2) = [2, 4]
call f(size(y), y)

contains

    subroutine f(n, A)
    integer, intent(in) :: A(n)
    integer, intent(in) :: n
    print *, A
    end subroutine

end program

```

Every compiler so far that I tried compiles it without any error.

I’ve seen the above usage in codes (not mine :), so I would like to understand how it should be handled by a compiler. If it is implicitly typed, then I would think the above code should not compile, since I used `implicit none`.

---

<div class="post-metadata">

**Author:** ![tyranids](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tyranids](https://fortran-lang.discourse.group/u/tyranids)\
**Post date:** [February 11, 2024, 7:20am UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/2 "2024-02-11T07:20:33Z")

</div>

My understanding was that it is the compiler’s job to determine which order variables _actually_ need to be declared in, and their order at the top of a module/suborogram/program itself was meaningless.

In your example, n is never implicitly typed, since it is an integer as you requested in the same declaration section at the top.

---

<div class="post-metadata">

**Author:** ![mhulsen](https://avatars.discourse-cdn.com/v4/letter/m/ea666f/32.png) [@mhulsen](https://fortran-lang.discourse.group/u/mhulsen)\
**Post date:** [February 11, 2024, 7:28am UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/3 "2024-02-11T07:28:30Z")

</div>

Nagfor gives:  
NAG Fortran Compiler Release 7.1(Hanzomon) Build 7144  
Error: t.f90, line 9: Implicit type for N  
detected at N@)  
Error: t.f90, line 10: Symbol N has already been implicitly typed  
detected at N@  
[NAG Fortran Compiler pass 1 error termination, 2 errors]

---

<div class="post-metadata">

**Author:** ![Harper](https://avatars.discourse-cdn.com/v4/letter/h/b5ac83/32.png) [@Harper](https://fortran-lang.discourse.group/u/Harper)\
**Post date:** [February 11, 2024, 8:37am UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/4 "2024-02-11T08:37:27Z")

</div>

Alas @tyranids, sometimes the order of declarations does matter. Always in `parameter` statements defining constants whose values may depend on previously defined constants ,and sometimes when defining variables. For example, Modern Fortran Explained (2023 edition) p22 says a derived type may have a component that is of a previously defined derived type. Or see the f2023 standard section 7.3.2.2 paragraph 2.

---

<div class="post-metadata">

**Author:** ![jkd2022](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/jkd2022/32/1949_2.png) [@jkd2022](https://fortran-lang.discourse.group/u/jkd2022)\
**Post date:** [February 11, 2024, 9:09am UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/5 "2024-02-11T09:09:43Z")

</div>

BLAS and LAPACK do this with leading dimensions in the argument list, e.g.:

```auto
SUBROUTINE DGEMV(TRANS,M,N,ALPHA,A,LDA,X,INCX,BETA,Y,INCY)

```

LDA comes after A and A is defined as

```auto
DOUBLE PRECISION A(LDA,*)

```

I’ve always thought this ordering odd, but no compiler has complained so far.

Edit: LDA does appear before A in the declaration section, which is the point of the OP.

---

<div class="post-metadata">

**Author:** ![PierU](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pieru/32/1848_2.png) [@PierU](https://fortran-lang.discourse.group/u/PierU)\
**Post date:** [February 11, 2024, 11:26am UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/7 "2024-02-11T11:26:13Z")

</div>

Although I haven’t dug in the standard, I have always heard that declaration order matters. If your code example is compiled with `gfortran -pedantic`, one gets the following warning message:

```plaintext
   10 | integer, intent(in) :: A(n)
      | 1
Warning: GNU Extension: Symbol 'n' is used before it is typed at (1)

```

The fact is also that most compilers do accept out-of-order declarations, but this is non standard I believe.

---

<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:** [February 11, 2024, 12:20pm UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/8 "2024-02-11T12:20:10Z")

</div>

> [@certik](#):
>
> Every compiler so far that I tried compiles it without any error.

When using multiple compilers to test that a code is standard-conforming, one should use their standard-conformance options. Gfortran and ifort compile your code by default, but `gfortran -std=f2018 certik.f90`

gives

```auto
certik.f90:9:29:

    9 | integer, intent(in) :: A(n)
      | 1
Error: GNU Extension: Symbol 'n' is used before it is typed at (1)
certik.f90:8:21:

    8 | subroutine f(n, A)
      | 1
Error: Symbol 'a' at (1) has no IMPLICIT type
certik.f90:4:18:

    4 | call f(size(y), y)
      | 1
Error: Type mismatch in argument 'a' at (1); passed INTEGER(4) to UNKNOWN

```

and `ifort -stand:f18 certik.f90` gives

```auto
certik.f90(9): warning #8586: Implicit type is given to allow out-of-order declaration. Non-standard extension. [N]
    integer, intent(in) :: A(n)
-----------------------------^

```

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [February 11, 2024, 3:12pm UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/9 "2024-02-11T15:12:42Z")

</div>

Yes, the first appearance of `n` here is an implicit declaration. There is then:

> A variable in a specification expression shall have its type and type parameters, if any, specified by a previous declaration in the same scoping unit, by the implicit typing rules in effect for the scoping unit, or by host or use association. If a variable in a specification expression is typed by the implicit typing rules, its appearance in any subsequent type declaration statement shall confirm the implied type and type parameters. If a specification inquiry depends on the type of an object of derived type, that type shall be previously defined.(F2023 10.1.11p6)

Notice the use of “previous declaration” (which may be in the same declaration statement), and that if the variable is implicitly typed and then explicitly typed later, that explicit type must match the implicit type. Use of `IMPLICIT NONE` means use before declaration is an error, but as noted, many compilers let you get away with this as an extension.

On a related note, some compilers let you do:

```auto
integer foo
integer foo

```

At DEC, we had long arguments about this, as some customers wanted us to support it, but we held our ground and said no. The Intel compiler continues to disallow duplicate typing.

---

<div class="post-metadata">

**Author:** ![tyranids](https://avatars.discourse-cdn.com/v4/letter/t/3e96dc/32.png) [@tyranids](https://fortran-lang.discourse.group/u/tyranids)\
**Post date:** [February 11, 2024, 3:22pm UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/10 "2024-02-11T15:22:00Z")

</div>

You’re right. I forgot about the parameter thing, I’ve seen that for sure. Same with derived types needing to be in order, I forgot about that.

As others mentioned, there are compiler options that will start complaining if you have declarations out of order as well.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [February 11, 2024, 4:35pm UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/11 "2024-02-11T16:35:41Z")

</div>

@sblionel thank you for the clarification. The above example thus should not compile with `implicit none`, and NAG doesn’t compile it, and GFortran with `-std=f2018` or `-pedantic` also doesn’t compile it.

One more question regarding:

> “previous declaration” (which may be in the same declaration statement)

Definitely this is allowed:

```auto
    subroutine f(n, A)
    integer, intent(in) :: n, A(n)
    print *, A
    end subroutine

```

But how about this:

```auto
    subroutine f(n, A)
    integer, intent(in) :: A(n), n
    print *, A
    end subroutine

```

Here `n` comes later in the _same_ declaration statement. Is that allowed?

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [February 11, 2024, 5:00pm UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/12 "2024-02-11T17:00:36Z")

</div>

The second example is not allowed. I remember an interp for the first case, but am having trouble finding it. There is explicit wording in the standard for _similar_ cases, such as:

> A named constant shall not be referenced unless it has been defined previously; it may be defined previously in the same statement.

but I’ve not yet found a similar statement for specification expressions.

---

<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 11, 2024, 8:36pm UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/13 "2024-02-11T20:36:36Z")

</div>

Here is a small program that surprises some programmers. I think this is legal fortran, it is just a little tricky.

```auto
program xxx
   implicit none
   integer :: n=3, a(3)
   call yyy( a )
contains
   subroutine yyy( a )
      integer :: a(n)
      integer, parameter :: n=2
      write(*,*) 'size=', size(a)
      return
   end subroutine yyy
end program xxx

```

The tricky part is that there are two variables `n` within the subroutine, one used in the declaration that is host associated, and then the subsequent local parameter which thereafter masks the host variable. If the parameter statement is moved up a line, then a different value is printed.

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [February 11, 2024, 10:40pm UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/14 "2024-02-11T22:40:06Z")

</div>

> [@RonShepard](#):
>
> I think this is legal fortran,

It is not legal. For example, NAG complains:

```auto
NAG Fortran Compiler Release 7.1(Hanzomon) Build 7120
Error: t.f90, line 8: PARAMETER N referenced before definition
       detected at N@=

```

And Intel:  
Intel(R) Fortran Compiler for applications running on Intel(R) 64, Version 2024.0.2 Build 20231213  
Copyright (C) 1985-2023 Intel Corporation. All rights reserved.

```auto
t.f90(8): error #7157: This name has been incorrectly used in a specification expression of a contained procedure. [N]
      integer, parameter :: n=2
----------------------------^

```

There can never be one local name for two distinct entities.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [February 12, 2024, 6:39am UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/15 "2024-02-12T06:39:07Z")

</div>

Thank you @sblionel for all the clarifications. I think it’s clear to me now.

Thank you everybody in this thread for the discussion. Looks like I wasn’t the only one confused. But hopefully all is clear now.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [February 12, 2024, 6:41am UTC](https://fortran-lang.discourse.group/t/when-is-n-in-explicit-shape-array-a-n-implicitly-typed/7389/16 "2024-02-12T06:41:26Z")

</div>

This thread finally explained things for me as I’ve encountered these errors before and wondered what the problem was.

In C, you can also pass the length as an argument, but it must precede the array argument.

```auto
void foo(int n, int arr[n]) {
  // ...
}

```

As an extension, the GNU compiler supports [forward declaration](https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html), which looks like this:

```auto
void foo(int n; int arr[n], int n) {
  // ...
}

```

The first `n` before the semi-colon is a parameter forward declaration that serves the parsing phase, to make sure the `n` is known before the `arr` argument is parsed.
