Question about the function return type

I’m trying to write a simple Fibonacci app in Fortran, and it’s my first time with the language. I’m having an issue where I have a function which receives and returns an integer, yet the compiler says: Error: Return type mismatch of function ‘fibonacci’ at (1) (REAL(4)/INTEGER(4)).


program fibonacci_demo
        implicit none
        integer :: user_num
        integer :: fib_result

        print *, 'Enter a number: '
        read(*,*) user_num

        fib_result = fibonacci(user_num)

        print *, 'fibonacci(', user_num , ') = ', fib_result
end program

recursive function fibonacci(a) result(b)
        implicit none
        integer, intent(in) :: a
        integer :: b

        if (a <= 1) then
           b = 1
           b = fibonacci(a-1) + fibonacci(a-2)
        end if
end function fibonacci

What am I doing wrong with the function and/or data type?


replace end program with contains and add end program to the end of the file.

The program does not contain any declaration of the function fibonaccci, which should be one of the error messages you see. Always look at the first one, not the last one.

PS Welcome to the forum and good luck in your first adventures in Fortran.

1 Like

You can also use a module.

See Organising code structure - Fortran Programming Language
It will help.

Woah! Neat! Thank you :slight_smile:

I did see that, but it seemed that contains was for subroutines. Should have tried it though!

From ISO/IEC 1539 : 1991 (E) and later
"The CONTAINS statement separates the body of a main program, module, or subprogram from any internal or module subprograms it may contain. The CONTAINS statement is not executable. "

1 Like

The simplest, though not as elegant as hints above, is adding fibonacci to any of integer declarations in main program. Or, just a bit more elegant, a separate declaration
integer, external :: fibonacci

Is that something like an extern in C or an operator? How does it work?

No, in Fortran this declares an identifier to be a name of a procedure (subroutine or function). Mostly used to be able to send a procedure as an argument to another procedure. Required in old Fortran.

Now in Modern Fortran (90+) obviously not needed if one creates explicit interface of such a procedure (by use of interface block or by storing the procedure in a module or after contains statement, as an internal one)

Like a function pointer?

@rjzak , if you’re interested further in Fortran, please see this thread.

Not too long ago, a respondent on this thread of yours had asked about sample code to illustrate briefly the features of Fortran to those looking at Fortran for the first time. I had chosen a Fibonacci function example for it as you will see in the code in that thread! Hopefully you will find the code sample of some interest. You may know of free Intel oneAPI Fortran compiler you can try toward the example.

1 Like

Thank you. Fortran has always been in the back of my mind for historical reasons. I’ve been a programmer for a long time, in C/C++, Go, Java, Python, Rust. Never learned Fortran. I’m here because I found the recent paper on arxiv about the state of Fortran, so I wanted to investigate.


Yes, a procedure name, used as an actual argument, works as a ‘self-pointer’, very similar to how a function name is treated in C. As it has no parentheses in this context, it must be previously declared to be a subprogram, not an ordinary scalar variable.
Not to be confused with procedure pointers (Fortran 2003+) which can also be used for that purpose.
The difference between true procedure pointers in Fortran i C is, AFAIU, that a function identifier in C is equivalent to a pointer to that function. Not so in Fortran.