# Question about the function return type

**URL:** <https://fortran-lang.discourse.group/t/question-about-the-function-return-type/3188>\
**Category:** Help\
**Created:** [April 8, 2022, 6:53pm UTC](https://fortran-lang.discourse.group/t/question-about-the-function-return-type/3188 "2022-04-08T18:53:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjzak](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rjzak/32/1456_2.png) [@rjzak](https://fortran-lang.discourse.group/u/rjzak)\
**Post date:** [April 8, 2022, 6:53pm UTC](https://fortran-lang.discourse.group/t/question-about-the-function-return-type/3188/1 "2022-04-08T18:53:46Z")

</div>

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))`.

Code:

```auto
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
        else
           b = fibonacci(a-1) + fibonacci(a-2)
        end if
end function fibonacci

```

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

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [April 8, 2022, 7:01pm UTC](https://fortran-lang.discourse.group/t/question-about-the-function-return-type/3188/2 "2022-04-08T19:01:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [April 8, 2022, 7:02pm UTC](https://fortran-lang.discourse.group/t/question-about-the-function-return-type/3188/3 "2022-04-08T19:02:47Z")

</div>

You can also use a module.

See [Organising code structure - Fortran Programming Language](https://fortran-lang.org/learn/quickstart/organising_code)  
It will help.

---

<div class="post-metadata">

**Author:** ![rjzak](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rjzak/32/1456_2.png) [@rjzak](https://fortran-lang.discourse.group/u/rjzak)\
**Post date:** [April 8, 2022, 7:03pm UTC](https://fortran-lang.discourse.group/t/question-about-the-function-return-type/3188/4 "2022-04-08T19:03:08Z")

</div>

Woah! Neat! Thank you 🙂

> [@vmagnin](#):
>
> See [Organising code structure - Fortran Programming Language](https://fortran-lang.org/learn/quickstart/organising_code)  
> It will help.

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

---

<div class="post-metadata">

**Author:** ![alozada](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/alozada/32/298_2.png) [@alozada](https://fortran-lang.discourse.group/u/alozada)\
**Post date:** [April 8, 2022, 7:23pm UTC](https://fortran-lang.discourse.group/t/question-about-the-function-return-type/3188/5 "2022-04-08T19:23:07Z")

</div>

> [@rjzak](#):
>
> 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](https://gcc.gnu.org/wiki/GFortranStandards#Fortran_2008)  
"_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._ "

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [April 8, 2022, 9:54pm UTC](https://fortran-lang.discourse.group/t/question-about-the-function-return-type/3188/6 "2022-04-08T21:54:29Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![rjzak](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rjzak/32/1456_2.png) [@rjzak](https://fortran-lang.discourse.group/u/rjzak)\
**Post date:** [April 8, 2022, 10:03pm UTC](https://fortran-lang.discourse.group/t/question-about-the-function-return-type/3188/7 "2022-04-08T22:03:39Z")

</div>

> [@msz59](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [April 8, 2022, 10:22pm UTC](https://fortran-lang.discourse.group/t/question-about-the-function-return-type/3188/8 "2022-04-08T22:22:38Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![rjzak](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rjzak/32/1456_2.png) [@rjzak](https://fortran-lang.discourse.group/u/rjzak)\
**Post date:** [April 8, 2022, 10:37pm UTC](https://fortran-lang.discourse.group/t/question-about-the-function-return-type/3188/9 "2022-04-08T22:37:37Z")

</div>

> [@msz59](#):
>
> Mostly used to be able to send a procedure as an argument to another procedure.

Like a function pointer?

---

<div class="post-metadata">

**Author:** ![FortranFan](https://avatars.discourse-cdn.com/v4/letter/f/96bed5/32.png) [@FortranFan](https://fortran-lang.discourse.group/u/FortranFan)\
**Post date:** [April 8, 2022, 11:19pm UTC](https://fortran-lang.discourse.group/t/question-about-the-function-return-type/3188/10 "2022-04-08T23:19:59Z")

</div>

> [@rjzak](#):
>
> I’m trying to write a simple Fibonacci app in Fortran, and it’s my first time with the language …

@rjzak , if you’re interested further in Fortran, please see this [**thread**](https://fortran-lang.discourse.group/t/modern-fortran-sample-code/2019/4).

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**](https://fortran-lang.discourse.group/t/intel-releases-oneapi-toolkit-free-fortran-2018/471) you can try toward the example.

---

<div class="post-metadata">

**Author:** ![rjzak](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/rjzak/32/1456_2.png) [@rjzak](https://fortran-lang.discourse.group/u/rjzak)\
**Post date:** [April 9, 2022, 2:49am UTC](https://fortran-lang.discourse.group/t/question-about-the-function-return-type/3188/11 "2022-04-09T02:49:31Z")

</div>

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](https://arxiv.org/abs/2203.15110) about the state of Fortran, so I wanted to investigate.

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [April 9, 2022, 9:16am UTC](https://fortran-lang.discourse.group/t/question-about-the-function-return-type/3188/12 "2022-04-09T09:16:56Z")

</div>

> [@rjzak](#):
>
> Like a function pointer?

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.
