Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling non recursive function inside recursive one 2

Status
Not open for further replies.

chicken666

Programmer
Jun 25, 2009
3
PL
Can anybody tell me why i can't get correct result from non recursive function called inside recursive function

Here is an example

integer function test()
integer test
test = 69
return
end function test


integer recursive function testRec(i)
integer, intent(inout) :: i
integer :: testRec

print *, i
print *, test()
if(i <= 0) then
return
end if
i = i-1
testRec = testRec(i)
return
end function testRec


and the result

3
NaN
2
NaN
1
NaN
0
NaN


Why does function test return Nan instead of 69??
 
At a guess it doesn't know that test is an integer function. Try adding something like integer test
 
But everthing works when i invoke test function outside recursive testRec
 
Hi chicken666,

With the code you posted I got everytime this error from g95 compiler:
Code:
    testRec = testRec(i)
                     1
Error: Unexpected array reference at (1)

So I modified your code a little:
Code:
[COLOR=#a020f0]program[/color] chicken666
  [COLOR=#2e8b57][b]integer[/b][/color] :: [COLOR=#a020f0]result[/color]
  [COLOR=#a020f0]result[/color] [COLOR=#804040][b]=[/b][/color] testRec([COLOR=#ff00ff]3[/color])
  [COLOR=#0000ff]! end[/color]

[COLOR=#a020f0]contains[/color]
  [COLOR=#0000ff]! functions[/color]

  [COLOR=#2e8b57][b]integer[/b][/color] [COLOR=#a020f0]function[/color] test()
    test [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]69[/color]
  [COLOR=#a020f0]end function[/color] test

  [COLOR=#a020f0]recursive[/color] [COLOR=#a020f0]function[/color] testRec(i) [COLOR=#a020f0]result[/color](r)
    [COLOR=#2e8b57][b]integer[/b][/color] :: r
    [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: i

    [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color], i
    [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color], test()
    [COLOR=#804040][b]if[/b][/color](i [COLOR=#804040][b]<=[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
        [COLOR=#804040][b]return[/b][/color]
    [COLOR=#804040][b]end if[/b][/color]
    r [COLOR=#804040][b]=[/b][/color] testRec(i[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color])
  [COLOR=#a020f0]end function[/color] testRec
[COLOR=#a020f0]end program[/color] chicken666
and it works now:
Code:
$ g95 chicken666.f90 -o chicken666

$ chicken666
 3
 69
 2
 69
 1
 69
 0
 69
 
Without using contains you need zo declare the functions more than once, i.e. the function testRec() in the main pogram and the function test() in the function testRec(), so
Code:
[COLOR=#a020f0]program[/color] chicken666
  [COLOR=#2e8b57][b]integer[/b][/color] :: [COLOR=#a020f0]result[/color], testRec
  [COLOR=#a020f0]result[/color] [COLOR=#804040][b]=[/b][/color] testRec([COLOR=#ff00ff]3[/color])
  [COLOR=#0000ff]! end[/color]
[COLOR=#a020f0]end program[/color] chicken666

[COLOR=#0000ff]! functions[/color]

[COLOR=#2e8b57][b]integer[/b][/color] [COLOR=#a020f0]function[/color] test()
  test [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]69[/color]
[COLOR=#a020f0]end function[/color] test

[COLOR=#a020f0]recursive[/color] [COLOR=#a020f0]function[/color] testRec(i) [COLOR=#a020f0]result[/color](r)
  [COLOR=#2e8b57][b]integer[/b][/color] :: r, test
  [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: i

  [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color], i
  [COLOR=#804040][b]print[/b][/color] [COLOR=#804040][b]*[/b][/color], test()
  [COLOR=#804040][b]if[/b][/color](i [COLOR=#804040][b]<=[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
    [COLOR=#804040][b]return[/b][/color]
  [COLOR=#804040][b]end if[/b][/color]
  r [COLOR=#804040][b]=[/b][/color] testRec(i[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color])
[COLOR=#a020f0]end function[/color] testRec

Code:
$ g95 chicken666_2.f90 -o chicken666_2

$ chicken666_2
 3
 69
 2
 69
 1
 69
 0
 69
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top