SAL
SAL

Reputation: 1260

Can I pass a format statement to a subroutine in Fortran?

I'd like to be able to pass a 'format' into a Fortran subroutine. Take this example:

        write(6,1002) M1    
1002    format(A, "M1, Mach number at boundary layer edge", f8.3)

For reasons too involved to go into here it would be useful to have a generic function to which I send a variable with a format statement that would be used to write out. I can't figure out a way of doing this.

Any ideas?

Upvotes: 1

Views: 356

Answers (2)

Forget FORMAT statements, use format strings

   string = '(A, "M1, Mach number at boundary layer edge", f8.3)'
   write(*,string) M1

You can pass strings easily to a subroutine.

Format statements are just obsolete and awkward to work with.

Also, do not use unit number 6, but *, that is much more portable.

Upvotes: 1

High Performance Mark
High Performance Mark

Reputation: 78364

You can't really pass a format statement to a subroutine. But you can pass a format string, something like:

fmtstr = '(A, "M1, Mach number at boundary layer edge", f8.3)'
...
call mysub(args, fmtstr)

then

subroutine mysub(args, fmtstr)
    ...
    character(*), intent(in) :: fmtstr
    ...
    write(*,fmtstr) M1
end subroutine

Some might argue that format strings are the best modern way to handle formats in all circumstances and have retired their use of format statements entirely.

Upvotes: 4

Related Questions