- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have the following code, but it is only returning a real. Even though in the function (in this case, fib_result is integer 8, when it gets back to the main program it a bizarre real #. How can i return an integer?
RECURSIVE FUNCTION FIB (n) RESULT (fib_result)
INTEGER :: fib_result
If (n <= 2) then
fib_result = 1
else
fib_result = FIB(n-1) + FIB (n -2)
endif
END FUNCTION
PROGRAM FibRepeat
REAL :: elapsed_time, start, finish
INTEGER :: num, theresult
num = 6
write (6,11) "Final fibonacci value for input of ", num ," is ", FIB(num)
11 FORMAT (1x, A40, 2X, I3, 2x, A4, I9)
END PROGRAM FibRepeat
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You need to declare FIB as INTEGER in the main program. Implicitly, it is REAL because the name starts with F (A-H, O-Z). Since FIB is not a contained procedure nor from a module, the compiler doesn't know that you wrote it as INTEGER.
FWIW, if you built this in Intel Visual Fortran in Visual Studio, you should have seen:
t.f90(33): error #7977: The type of the function reference does not match the type of the function definition. [FIB]
write (6,11) "Final fibonacci value for input of ", num ," is ", FIB(num)
-----------------------------------------------------------------^
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page