This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
Re: Errors with optional arguments
- From: Salvatore Filippone <salvatore dot filippone at uniroma2 dot it>
- To: fortran at gcc dot gnu dot org
- Date: Fri, 18 Dec 2009 10:56:54 +0100
- Subject: Re: Errors with optional arguments
- Reply-to: salvatore dot filippone at uniroma2 dot it
Ignacio Fernández Galván <jellby at yahoo dot com> wrote:
I encountered a couple of errors when trying to use optional arguments
in subroutines. First, this one happens when the optional argument is
the last of three, but not the only one or the last of two:
==========================================
$ cat test.f90
program test
implicit none
real :: a,b,c
call one(a)
write(6,*) a
call one()
write(6,*) a
call two(a,b)
write(6,*) b
call two(a)
write(6,*) b
call three(a,b,c)
write(6,*) c
call three(a,b)
write(6,*) c
end program test
-----------------------------------
Your code is wrong. You CANNOT use optional arguments without an INTERFACE.
One solution would be to put the code into a module, as follows
module foo
contains
subroutine one(a)
implicit none
real,optional :: a
if (present(a)) a=0.0
end subroutine one
subroutine two(a,b)
implicit none
real :: a
real,optional :: b
if (present(b)) b=0.0
end subroutine two
subroutine three(a,b,c)
implicit none
real :: a,b
real,optional :: c
if (present(c)) c=0.0
end subroutine three
end module foo
program test
use foo
implicit none
.......
end program test
HTH
Salvatore