From: monir on
Hello;

1) Let us say I've a routine mySort() which sorts four 4D array
variables in increasing order wrt there abscissa:
...abscissa x(i,j,k,m)
...abscissa y(i,j,k,m)
...abscissa z(i,j,k,m)
...abscissa t(i,j,k,m)
.....variable vx(i,j,k,m)
.....variable vy(i,j,k,m)
.....variable vz(i,j,k,m)
.....variable vt(i,j,k,m)
...i=1,maxi, j=1, maxj, k=1, maxk, m=1, maxm

2) The calling statement would be:
Call mySort (x,vx,maxi,
y,vy,maxj,
z,vz,maxk,
t,vt,maxm)
(you'll see in a moment why the argument list is arranged this way)

3) Now suppose instead of a 4-variable sort, I'm interested in only
three 3D variables or two 2D variables or even one 1D variable sort.
Is there a way in Fortran to use the same routine mySort() in this
case (F77/g95) ??
Is my question related to Rank and/or Dynamic Array feature (F90 and
later) ??
If it is not possible in Fortran, is there a better way than writing 4
almost identical routines; one for each case ??

4) If I recall correctly, some programming languages/compilers allow
the use of "Optional" argument in the calling statement if the
argument is not needed. However, once an argument is made Optional,
all the arguments that follow must be made Optional as well.
(and that's why the argument list in Item 2 above was arranged that
way)
For example, for a 3-variable sort, the call statement may look
something like:
Call mySort (x,vx,maxi,
y,vy,maxj,
z,vz,maxk,
optional t,optional vt,optional maxm)

5) I've searched the net using keywords: fortran+optional, optional
+argument, etc. with no results directly relevant.

Could someone please comment and provide some guidance on how to.

Thank you kindly.
Monir
From: dpb on
monir wrote:
....

> 3) Now suppose instead of a 4-variable sort, I'm interested in only
> three 3D variables or two 2D variables or even one 1D variable sort.
> Is there a way in Fortran to use the same routine mySort() in this
> case (F77/g95) ??
> Is my question related to Rank and/or Dynamic Array feature (F90 and
> later) ??
> If it is not possible in Fortran, is there a better way than writing 4
> almost identical routines; one for each case ??
>
> 4) If I recall correctly, some programming languages/compilers allow
> the use of "Optional" argument in the calling statement if the
> argument is not needed. However, once an argument is made Optional,
> all the arguments that follow must be made Optional as well.
> (and that's why the argument list in Item 2 above was arranged that
> way)
....

OPTIONAL in F95+ has keyword option for argument association as well as
positional altho can be rather bulky to have to use it for more than an
argument or two from a coding standpoint.

Read the documentation for the compiler OPTIONAL statement and
attributes and/or consult a text.

--
From: Jim Xia on
> For example, for a 3-variable sort, the call statement may look
> something like:
> Call mySort (x,vx,maxi,
>                    y,vy,maxj,
>                    z,vz,maxk,
>                    optional t,optional vt,optional maxm)
>
> 5) I've searched the net using keywords: fortran+optional, optional
> +argument, etc. with no results directly relevant.
>
> Could someone please comment and provide some guidance on how to.


The optional keyword has to be specified on the interface declaration,
not the caller (the call side).

If you treat 3-D array the same as 4-D variable except the last
dimension is restricted to have only one element, then your original
code can still be used. All you need is to modify the declarations of
all the dummy arguments.


If you'd like to learn how to do this in coding, please post the
original interface for mySort.


Cheers,

Jim
From: Reinhold Bader on
Hello,

Am 22.02.2010 15:28, schrieb monir:
> Hello;
>
> 1) Let us say I've a routine mySort() which sorts four 4D array
> variables in increasing order wrt there abscissa:
> ..abscissa x(i,j,k,m)
> ..abscissa y(i,j,k,m)
> ..abscissa z(i,j,k,m)
> ..abscissa t(i,j,k,m)
> ....variable vx(i,j,k,m)
> ....variable vy(i,j,k,m)
> ....variable vz(i,j,k,m)
> ....variable vt(i,j,k,m)
> ..i=1,maxi, j=1, maxj, k=1, maxk, m=1, maxm
>
> 2) The calling statement would be:
> Call mySort (x,vx,maxi,
> y,vy,maxj,
> z,vz,maxk,
> t,vt,maxm)
> (you'll see in a moment why the argument list is arranged this way)
>
> 3) Now suppose instead of a 4-variable sort, I'm interested in only
> three 3D variables or two 2D variables or even one 1D variable sort.
> Is there a way in Fortran to use the same routine mySort() in this
> case (F77/g95) ??

If the subroutine has the interface


subroutine mysort(absc, values, nmax)
real :: absc(*)
real :: values(*)
integer :: nmax
..
end subroutine

you can also call this with arguments of rank 2 or higher:

real :: absc(m, n), values(n, m)
:
call mysort(absc(1,1), values(1,1), m*n)

Note that the first array elements are specified as arguments here.

> Is my question related to Rank and/or Dynamic Array feature (F90 and
> later) ??
> If it is not possible in Fortran, is there a better way than writing 4
> almost identical routines; one for each case ??
>
> 4) If I recall correctly, some programming languages/compilers allow
> the use of "Optional" argument in the calling statement if the
> argument is not needed. However, once an argument is made Optional,
> all the arguments that follow must be made Optional as well.

Fortran allows to specify the OPTIONAL attribute on a dummy argument.
It is not required that any dummy following the first one itself
must have the OPTIONAL attribute. Note that it is recommended to use
the PRESENT intrinsic to check whether an actual argument was
specified:


subroutine foo(x, ...)
real, optional :: x
:
if (present(x)) then
... ! reference and/or define x
end if
:
end subroutine

However, for the scenario you describe here optional arguments are
probably not a good choice.

> (and that's why the argument list in Item 2 above was arranged that
> way)
> For example, for a 3-variable sort, the call statement may look
> something like:
> Call mySort (x,vx,maxi,
> y,vy,maxj,
> z,vz,maxk,
> optional t,optional vt,optional maxm)
>
> 5) I've searched the net using keywords: fortran+optional, optional
> +argument, etc. with no results directly relevant.
>
> Could someone please comment and provide some guidance on how to.
>
> Thank you kindly.
> Monir


Regards
Reinhold
From: Richard Maine on
Jim Xia <jimxia(a)hotmail.com> wrote:

> The optional keyword has to be specified on the interface declaration,
> not the caller (the call side).

Well, that seems a bit misleading. It has to be specified in the actual
code for the procedure. That's the important part. There doesn't even
need to be an "interface declaration", except to the extent that you
mean the declarations in the procedure itself. Hmm. It does occur to me
that you might well mean that. But mostly I deduce that just because I'd
expect you to know it rather than because the words tell me that. I bet
a lot of people would misread the words as referring to an interface
body.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain