Errors with optional arguments
Salvatore Filippone
salvatore.filippone@uniroma2.it
Fri Dec 18 09:56:00 GMT 2009
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
More information about the Fortran
mailing list