If there is no type, you will not be able to determine if the returned value is an INTEGER, a REAL or something else.FUNCTION DoSomething(a, b) IMPLICIT NONE INTEGER, INTENT(IN) :: a, b DoSomthing = SQRT(a*a + b*b) END FUNCTION DoSomthing
Actually, this is not an error. But, without INTENT(IN), our Fortran compiler will not be able to check many potential errors for you.REAL FUNCTION DoSomething(a, b) IMPLICIT NONE INTEGER :: a, b DoSomthing = SQRT(a*a + b*b) END FUNCTION DoSomthing
Since formal argument a is declared with INTENT(IN), its value cannot be changed.REAL FUNCTION DoSomething(a, b) IMPLICIT NONE INTEGER, INTENT(IN) :: a, b IF (a > b) THEN a = a - b ELSE a = a + b END IF DoSomthing = SQRT(a*a + b*b) END FUNCTION DoSomthing
When the execution of this function reaches END FUNCTION, it returns the value stored in DoSomething. However, in the above, since there is no value ever stored in DoSmething, the returned value could be anything (i.e., a garbage value).REAL FUNCTION DoSomething(a, b) IMPLICIT NONE INTEGER, INTENT(IN) :: a, b INTEGER :: c c = SQRT(a*a + b*b) END FUNCTION DoSomthing
In the above, function name DoSomething appears in the right-hand side of the second expression. This is a mistake. Only a special type of functions, RECURSIVE functions, could have their names in the right-hand side of expressions.REAL FUNCTION DoSomething(a, b) IMPLICIT NONE INTEGER, INTENT(IN) :: a, b DoSomething = a*a + b*b DoSomething = SQRT(DoSomething) END FUNCTION DoSomthing
In the above, the value of SQRT(a*a-b*b) rather than the value of a*a + b*b is returned. In fact, this is obvious. Since the name of a function can be considered as a special variable name, the second assignment overwrites the previous value.REAL FUNCTION DoSomething(a, b) IMPLICIT NONE INTEGER, INTENT(IN) :: a, b DoSomething = a*a + b*b DoSomething = SQRT(a*a - b*b) END FUNCTION DoSomthing