Variable length argument list in Fortran?
Asked Answered
U

1

6

Has Fortran ever gotten around to handling subroutine argument lists with arbitrary length, like C can do? (BTW, "present" isn't going to work for what I am trying to do.) Thanks.

Unclench answered 30/8, 2018 at 14:51 Comment(6)
This other question is about interoperability with C procedures with variable arguments. It is relevant but doesn't explicitly say "no" in the more general case.Risner
@Risner OK. Thanks.Unclench
Are the variadic arguments possibly (fortunately) of the same type? (e.g. all are reals etc) Then we may be able to use an array argument (though not a general solution...)Aphrodisiac
@Aphrodisiac Actually, yes, all arguments should be of the same type. Suggestions? Thanks.Unclench
Not anything special, but just passing an array as "call mysub( arg1, arg2, [ x1, x2, x3 ] )" etc, and define the routine such that the 3rd argument (in this case) is declared as "real, intent(in) :: xs( : )" etc. If we want to modify the actual args, it may be necessary to create an independent array (not a temporal one) and pass it in the same way (here intent(in) is not attached).Aphrodisiac
Variadic arguments always need an extra parameter to describe the type. You can get around it with transfer functions and arrays but it is quite clumsy. Optional arguments may be what you are looking for.Yes
R
6

There are no such subroutines in Fortran.

The syntax rule for a subroutine statement in Fortran 2008 is (12.6.2.3, R1235):

[ prefix ] SUBROUTINE subroutine-name [ ( [ dummy-arg-list ] ) [ proc-language-binding-spec ] ]

where dummy-arg-list is a list (in assumed syntax rule terms) of dummy-args. A dummy-arg is (R1235) either a name or a literal *.

[Before we get too excited about the possibility of variadic support, the * refers, of course, to an alternate return indicator.]

A list (R101) still refers to a well-defined (at source time) number of items.

There is a stated restriction regarding interoperability with C, that (15.3.7) the C prototype

... does not have variable arguments as denoted by the ellipsis (...)

Similar arguments apply to (Fortran) functions.

Risner answered 30/8, 2018 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.