# Error in interface block: syntax error

**URL:** <https://fortran-lang.discourse.group/t/error-in-interface-block-syntax-error/5362>\
**Category:** Help\
**Created:** [March 9, 2023, 7:05am UTC](https://fortran-lang.discourse.group/t/error-in-interface-block-syntax-error/5362 "2023-03-09T07:05:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![snano](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@snano](https://fortran-lang.discourse.group/u/snano)\
**Post date:** [March 9, 2023, 7:05am UTC](https://fortran-lang.discourse.group/t/error-in-interface-block-syntax-error/5362/1 "2023-03-09T07:05:58Z")

</div>

Here is code taken from wikibooks: [wikibooks](https://en.wikibooks.org/wiki/Fortran/Fortran_procedures_and_functions)

```auto
real function tester(a)
    real, intent (in), optional :: a
    if (present(a)) then
        tester = a
    else
        tester = 0.0
    end if
end function 

program main
    interface
        function tester(a)
            real function tester(a)
            real, intent (in), optional :: a
        end function 
    end interface

    print *, "[no args] tester() :", tester() ! yields: 0.0
    print *, "[args] tester(1.0):", tester(1.0) ! yields: 1.0
end program

```

Here is the error:

> gfortran -o optional\_arg.x optional\_arg.f90  
> optional\_arg.f90:13:25:
> 
> 13 | real function tester(a)  
> | 1  
> Error: Syntax error in data declaration at (1)

---

<div class="post-metadata">

**Author:** ![Euler-37](https://avatars.discourse-cdn.com/v4/letter/e/b19c9b/32.png) [@Euler-37](https://fortran-lang.discourse.group/u/Euler-37)\
**Post date:** [March 9, 2023, 7:31am UTC](https://fortran-lang.discourse.group/t/error-in-interface-block-syntax-error/5362/2 "2023-03-09T07:31:54Z")

</div>

```auto
real function tester(a)
    real, intent (in), optional :: a
    if (present(a)) then
        tester = a
    else
        tester = 0.0
    end if
end function 

program main
    interface
        real function tester(a)
            real, intent (in), optional :: a
        end function 
    end interface

    print *, "[no args] tester() :", tester() ! yields: 0.0
    print *, "[args] tester(1.0):", tester(1.0) ! yields: 1.0
end program

```

wikibook is wrong.

---

<div class="post-metadata">

**Author:** ![snano](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@snano](https://fortran-lang.discourse.group/u/snano)\
**Post date:** [March 9, 2023, 7:39am UTC](https://fortran-lang.discourse.group/t/error-in-interface-block-syntax-error/5362/3 "2023-03-09T07:39:56Z")

</div>

I think (not sure) the correct one is:

```auto
real function tester(a)
    real, intent (in), optional :: a
    if (present(a)) then
        tester = a
    else
        tester = 0.0
    end if
end function

program main
    interface
        function tester(a)
            real :: tester ! this one!!
            real, intent (in), optional :: a
        end function
    end interface

    print *, "[no args] tester() :", tester() ! yields: 0.0
    print *, "[args] tester(1.0):", tester(1.0) ! yields: 1.0
end program

```

with the output:

> gfortran -o optional\_arg.x optional\_arg.f90 && ./optional\_arg.x  
> [no args] tester() : 0.00000000  
> [args] tester(1.0): 1.00000000

---

<div class="post-metadata">

**Author:** ![snano](https://avatars.discourse-cdn.com/v4/letter/s/fbc32d/32.png) [@snano](https://fortran-lang.discourse.group/u/snano)\
**Post date:** [March 9, 2023, 7:49am UTC](https://fortran-lang.discourse.group/t/error-in-interface-block-syntax-error/5362/4 "2023-03-09T07:49:41Z")

</div>

I checked the integer version of the above program:

```auto
integer function tester(a)
    real, intent (in), optional :: a
    if (present(a)) then
        tester = a
    else
        tester = 0
    end if
end function

program main
    interface
        function tester(a)
            integer :: tester ! this one
            real, intent (in), optional :: a
        end function
    end interface

    print *, "[no args] tester() :", tester() ! yields: 0
    print *, "[args] tester(1.0):", tester(1.0) ! yields: 1
end program

```

output:

> gfortran -o optional\_arg\_integer.x optional\_arg\_integer.f90 && ./optional\_arg\_integer.x  
> [no args] tester() : 0  
> [args] tester(1.0): 1

So the  
statement `integer :: tester `  
is needed, otherwise syntax error comes.

thank you @Euler-37  
bests!

---

<div class="post-metadata">

**Author:** ![seamsay](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@seamsay](https://fortran-lang.discourse.group/u/seamsay)\
**Post date:** [March 9, 2023, 10:28am UTC](https://fortran-lang.discourse.group/t/error-in-interface-block-syntax-error/5362/5 "2023-03-09T10:28:45Z")

</div>

They’re both equivalent.

```f90
real function tester(a)

```

is just syntax sugar for

```f90
function tester(a)
    real :: tester

```

---

<div class="post-metadata">

**Author:** ![VladimirF](https://avatars.discourse-cdn.com/v4/letter/v/e9a140/32.png) [@VladimirF](https://fortran-lang.discourse.group/u/VladimirF)\
**Post date:** [March 9, 2023, 11:28am UTC](https://fortran-lang.discourse.group/t/error-in-interface-block-syntax-error/5362/6 "2023-03-09T11:28:46Z")

</div>

I don’t really like how the chapter shows advanced features of procedures but almost does not use modules and does most with interface blocks.

---

<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:** [March 9, 2023, 7:14pm UTC](https://fortran-lang.discourse.group/t/error-in-interface-block-syntax-error/5362/7 "2023-03-09T19:14:17Z")

</div>

Inadvertently or intentionally, someone at Wikibooks can confuse the heck out of a new reader and post an example like so:

```fortran
module tester_m
   interface
      module function tester(a) result(r) bind(C, name="TESTER")
          real, intent (in), optional :: a
          real :: r
      end function 
   end interface
end module tester_m

submodule(tester_m) tester_sm
contains
   module function tester(a) result(r) bind(C, name="TESTER")
      real, intent (in), optional :: a
      if (present(a)) then
        r = a
      else
        r = 0.0
      end if
   end function 
end submodule tester_sm

program main
   interface
      function tester(a) result(r) bind(C, name="TESTER")
          real, intent (in), optional :: a
          real :: r
      end function 
   end interface
   print *, "[no args] tester() :", tester() ! yields: 0.0
   print *, "[args] tester(1.0):", tester(1.0) ! yields: 1.0
end program

```

And get an rating of “almost evil” with the duplication of interfaces and names and the “cheating” with the global name! 😜

---

<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:** [March 9, 2023, 7:16pm UTC](https://fortran-lang.discourse.group/t/error-in-interface-block-syntax-error/5362/8 "2023-03-09T19:16:12Z")

</div>

> [@snano](#):
>
> Here is code taken from wikibooks: [wikibooks](https://en.wikibooks.org/wiki/Fortran/Fortran_procedures_and_functions)

+1 to that Wikibooks example for eschewing `implicit none` though!
