next up previous contents
Next: Recursive Procedures Up: External Procedures Previous: Required Interfaces

 

Side Effect Functions

If, in the function invocation

    rezzy = funky1(a,b,c) + funky2(a,b,c)

both funky1 and funky2 modify the value of a and a is used in the calculation of the result, then the order of execution would be important. Consider the following two internal functions:

    INTEGER FUNCTION funky1(a,b,c)
     REAL, INIENT(INOUT) :: a
     REAL, INIENT(IN)    :: b,c
      a = a*a
      funky1 = a/b
    END FUNCTION funky1

and

    INTEGER FUNCTION funky2(a,b,c)
     REAL, INIENT(INOUT) :: a
     REAL, INIENT(IN)    :: b,c
      a = a*2
      funky2 = a/c
    END FUNCTION funky2

Notice how both functions modify the value of a, this means that the value of rezzy is wholly dependent on the (undefined) order of execution.

With a=4, b=2 and c=4 the following happens:

A properly constructed function should be such that its result is uniquely determined by the values of its arguments, and the values of its arguments should be unchanged by any invocation as should any global entities of the program. Fortran 95 will introduce the PURE keyword which when applied to a procedure asserts that the routine is `side-effect-free' in the sense that it does not change any values behind the scenes. (For example, a PURE function will not change any global data nor will it change the values of any of its arguments.) If at all possible all functions should be PURE as this will keep the code simpler.

Return to corresponding overview page gif


next up previous contents
Next: Recursive Procedures Up: External Procedures Previous: Required Interfaces

©University of Liverpool, 1997
Wed May 28 20:20:27 BST 1997
Not for commercial use.