Anzzi
Anzzi

Reputation: 35

fortran type missmatch calling function

I cant figure out whats my mistake I googled the problem took "implicit none" and declared eyery variable to integer I used but I still get the follwing errors:

main.f95:37.20:

    e = Collatzf(i)
                1
Error: Return type mismatch of function 'collatzf' at (1) (UNKNOWN/INTEGER(4))
main.f95:37.12:

    e = Collatzf(i)
        1
Error: Function 'collatzf' at (1) has no IMPLICIT type

Here is my Code:

INTEGER FUNCTION Collatzf(n)

    IMPLICIT NONE
    INTEGER :: n, z

    z = 0

    DO WHILE(n /= 1)

        IF (MOD(n, 2) == 0) THEN
            n = n / 2
        ELSE
            n = 3 * n + 1
        END IF

        z = z + 1

    END DO

    Collatzf = z

END FUNCTION Collatzf


PROGRAM ProjectEuler14

    IMPLICIT NONE
    INTEGER :: lsg, e, s, i

    lsg = 0
    e = 0
    s = 0
    i = 2

    DO WHILE(i <= 1000000)

        e = Collatzf(i)

        IF(e > lsg) THEN
            lsg = e
            s = i
        END IF

        i = i + 1

    END DO

    WRITE(*, *) s, i

END PROGRAM ProjectEuler14

Thx :)

Upvotes: 0

Views: 381

Answers (2)

makeitshort
makeitshort

Reputation: 16

lanH's answer is correct. Three suggested solutions are:

1) "Provide a declaration of the type of Collatzf inside the main program", which means adding

INTEGER :: Collatzf

statement to the variable declaration in PROGRAM ProjectEuler14.

2) "Provide an interface body for Collatzf function", which means means adding

INTERFACE 
   FUNCTION Collatzf (i)
     INTEGER :: Collatzf
     INTEGER, INTENT(IN) :: i
   END FUNCTION Collatzf
END INTERFACE

statements to the variable declaration in "PROGRAM ProjectEuler14".

3) "Make that function a module procedure, and then USE the module inside the main program", which means creating a new file, named (for simplicity) "functions.f90":

MODULE functions

CONTAINS

  INTEGER FUNCTION Collatzf(n)

    IMPLICIT NONE
    INTEGER :: n, z

    z = 0

    DO WHILE(n /= 1)

       IF (MOD(n, 2) == 0) THEN
          n = n / 2
       ELSE
          n = 3 * n + 1
       END IF

       z = z + 1

    END DO

    Collatzf = z

  END FUNCTION Collatzf

END MODULE functions

Then compile functions.f90 first by e.g.:

gfortran -c functions.f90

and link the compiled "functions" module into your main program:

gfortran main.f90 functions.o

Upvotes: 0

IanH
IanH

Reputation: 21431

There is no declaration for the Collatzf function inside the main program.

Program units in Fortran have a separate compilation model - when compiling a program unit the compiler technically knows nothing about other program units, unless there are statements that give it explicit knowledge about those other units. So when compiling your main program (from the PROGRAM ... statement through to the END PROGRAM statement) the compiler has no idea what Collatzf is, even though the definition of that external function immediately preceded the main program. It cannot apply implicit typing rules because you have specified IMPLICIT NONE (a good thing), so hence you see the second error.

Provide a declaration of the type of Collatzf inside the main program. Better than that - provide an interface body for that function inside the main program. Even better than that again - make that function a module procedure, and then USE the module inside the main program.

Upvotes: 1

Related Questions