From: ajay on
On Mar 25, 11:59 pm, Aris Tofanis <aris.tofa...(a)6zbp6h.invalid> wrote:
> m_b_metcalf <michaelmetc...(a)compuserve.com> wrote:
> > On Mar 25, 6:55=A0am, ajay <ajay.rawa...(a)gmail.com> wrote:
> >> how can we implement this in fortran 77.
> >> i know there is intrinsic recursive function in fortran 90/95.
>
> >> function func(x,y)
> >> ...
> >> ...
> >> if ( y.lt.1.0d0 ) then
>
> >> func = ........
>
> >> elseif ( y.gt.1.0d0 ) then
>
> >> func = something/func(x,-y)
>
> >> end if
>
> >> end function
>
> >> waiting for the reply.....
> >> thanking you.....
>
> > Basically, you can't. But if you just add the recursive keyword and
> > use a modern compiler, it will work.
>
> I have never seen problems with g77 when creating a wrapper routine,
> which only calls the routine you want to call recursively:
>
> function wrapfunc(x,y)
> wrapfunc = func(x,y)
> end
>
> and call wrapfunc inside func. But I also never felt quite comfortable
> with this solution.

I came across very intersting example from : http://www.esm.psu.edu/~ajm138/fortranexamples.html

PROGRAM MAIN
INTEGER N, X
EXTERNAL SUB1
COMMON /GLOBALS/ N
X = 0
PRINT *, 'Enter number of repeats'
READ (*,*) N
CALL SUB1(X,SUB1)
END

SUBROUTINE SUB1(X,DUMSUB)
INTEGER N, X
EXTERNAL DUMSUB
COMMON /GLOBALS/ N
IF(X .LT. N)THEN
X = X + 1
PRINT *, 'x = ', X
CALL DUMSUB(X,DUMSUB)
END IF
END
From: Colin Watters on

"ajay" <ajay.rawat83(a)gmail.com> wrote in message
news:5225f53a-d8a2-487a-8426-1f5b4c0835e2(a)l24g2000prh.googlegroups.com...
On Mar 26, 9:34 am, glen herrmannsfeldt <g...(a)ugcs.caltech.edu> wrote:
> Phred Phungus <Ph...(a)example.invalid> wrote:
> > Why can't I emulate a pdp on ubuntu, other than not having
> > any idea how to do so?
>
> You can do that, but you can also get free accounts
> on PDPplanet.
>
> -- glen

I came across very intersting example from :
http://www.esm.psu.edu/~ajm138/fortranexamples.html

PROGRAM MAIN
INTEGER N, X
EXTERNAL SUB1
COMMON /GLOBALS/ N
X = 0
PRINT *, 'Enter number of repeats'
READ (*,*) N
CALL SUB1(X,SUB1)
END

SUBROUTINE SUB1(X,DUMSUB)
INTEGER N, X
EXTERNAL DUMSUB
COMMON /GLOBALS/ N
IF(X .LT. N)THEN
X = X + 1
PRINT *, 'x = ', X
CALL DUMSUB(X,DUMSUB)
END IF
END


....From the text accompanying this example:

'I've read in some texts that indirect reference is not allowed, but ask
yourself "How can SUB1 possibly know that it's actually calling itself when
it calls the dummy subroutine?" It can't, and thus this must work. In fact,
on every system I've tried so far, this routine works perfectly.'

....Clearly, the author never tried it on a CDC mainframe. The opsys and
Fortran (FORTRAN, in fact) compiler on these beasts was notable for not
using a stack. Recursion on such machines works fine up until the point
where you are finished calling, and start returning.

--
Qolin

Email: my qname at domain dot com
Domain: qomputing


From: Richard Maine on
Colin Watters <boss(a)qomputing.com> wrote:
[cute example of recursion elided]

> ...From the text accompanying this example:
>
> 'I've read in some texts that indirect reference is not allowed, but ask
> yourself "How can SUB1 possibly know that it's actually calling itself when
> it calls the dummy subroutine?" It can't, and thus this must work. In fact,
> on every system I've tried so far, this routine works perfectly.'
>
> ...Clearly, the author never tried it on a CDC mainframe. The opsys and
> Fortran (FORTRAN, in fact) compiler on these beasts was notable for not
> using a stack. Recursion on such machines works fine up until the point
> where you are finished calling, and start returning.

The author also clearly completely misses the point of the restriction
against recursion. That restriction isn't just in order to intentionally
make life difficult. It is there because recursion might not work
correctly without a lot of special effort on some systems.

To argue that something must work just because the compiler can't
diagnose it is simply nonsense. And, as I mentioned earlier in the
thread, testing could quite plausibly fail to find the problems.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: mecej4 on
On 3/26/2010 2:45 PM, Richard Maine wrote:
> Colin Watters<boss(a)qomputing.com> wrote:
> [cute example of recursion elided]
>
>> ...From the text accompanying this example:
>>
>> 'I've read in some texts that indirect reference is not allowed, but ask
>> yourself "How can SUB1 possibly know that it's actually calling itself when
>> it calls the dummy subroutine?" It can't, and thus this must work. In fact,
>> on every system I've tried so far, this routine works perfectly.'
>>
>> ...Clearly, the author never tried it on a CDC mainframe. The opsys and
>> Fortran (FORTRAN, in fact) compiler on these beasts was notable for not
>> using a stack. Recursion on such machines works fine up until the point
>> where you are finished calling, and start returning.
>
> The author also clearly completely misses the point of the restriction
> against recursion. That restriction isn't just in order to intentionally
> make life difficult. It is there because recursion might not work
> correctly without a lot of special effort on some systems.
>
> To argue that something must work just because the compiler can't
> diagnose it is simply nonsense.

Or, putting that in slightly different words, a belief that if one can
hoodwink the compiler the resulting code will work correctly if the
compiler is "good".

> And, as I mentioned earlier in the
> thread, testing could quite plausibly fail to find the problems.
>

-- mecej4
From: Tobias Burnus on
Am 25.03.2010 19:59, schrieb Aris Tofanis:
> I have never seen problems with g77 when creating a wrapper routine,
> which only calls the routine you want to call recursively:
>
> function wrapfunc(x,y)
> wrapfunc = func(x,y)
> end
>
> and call wrapfunc inside func. But I also never felt quite comfortable
> with this solution.

At least with gfortran but also with some other compilers it might fail
sometimes; gfortran puts large local arrays into static memory; but if
you use recursion (or call a procedure simultaneously vis OpenMP), all
the local variables of a routine need to be on the stack otherwise they
might override another.

Thus, using such a work around might lead to wrong results; and the bug
might be difficult to find (e.g. it only appears after one had increased
a local array by a small amount).

See "-frecursive", "-fmax-stack-var-size=" and "-fno-automatic" in the
gfortran manual.

Tobias