This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Errors with optional arguments


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



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]