1

Are we supposed to declare function names and type in the main program?

For example if i have a program named which has this header recursive function sum(a,b) result(result)

When i try to compile this, i get a few errors.

Error: Return type mismatch of function 'sum' at (1) (UNKNOWN/INTEGER(4))

Error: Function 'sum' at (1) has no IMPLICIT type

I managed to get rid of the errors by declaring the function in the main program for example:

integer :: sum

Why does this happen? Are we always supposed to do this?

Benjer
  • 153
  • 2
  • 10

1 Answers1

0

You should place your procedures in modules and use the modules. You than have the so called explicit interface to the procedures an the code that uses the module knows the description of the procedures.

Correct use of modules, subroutines and functions in fortran

When your function is not in a module, you indeed have to do what you shown, i.e., to declare

integer :: sum

This way the code knows sum() returns an integer. However it does not know what kind of arguments the function takes. The compiler cannot check if you use correct number and types of arguments when calling sum().

When it is in a module, the compiler can check the arguments and you do not have to declare the return type again.

Community
  • 1
  • 1