Re: Pure and internal procedures
- From: Erik Toussaint <user@xxxxxxxxxxxxxxxxxxx>
- Date: Fri, 09 Dec 2011 13:52:36 +0100
On 9-12-2011 4:12, Michael Trover wrote:
Think of it this way. This is an example using a function whose
interface we know and a code snippet.
Example 1:
interface
function f(x)
real, intent(in):: x
end function
end interface
x = 3.0
y = f(x)
z = f(x)
In this example the function invocations for y and z have to be done.
That is the code has to invoke the function twice. The second invocation
of f may not return the same value as the first invocation.
Example 2:
interface
pure function f(x)
real, intent(in):: x
end function
end interface
x = 3.0
y = f(x)
z = f(x)
Here the compiler knows, just from the interface, that it only needs to
invoke the function once and copy the result to both y and z. That's the
power of PURE.
Unless the variable y is host accessible by the pure function, and the function uses its value to calculate the result.
The following program prints
y = 3.0
z = 6.0
program pure_p
implicit none
real :: x, y, z
y = 0.0
x = 3.0
y = pure_f(x)
z = pure_f(x)
write(*, '("y = ", f0.1)') y
write(*, '("z = ", f0.1)') z
contains
pure function pure_f(x)
real, intent(in) :: x
real :: pure_f
pure_f = x + y
end function pure_f
end program pure_p
Erik.
.
- Follow-Ups:
- Re: Pure and internal procedures
- From: Michael Trover
- Re: Pure and internal procedures
- From: Michael Trover
- Re: Pure and internal procedures
- References:
- Pure and internal procedures
- From: Michael Trover
- Re: Pure and internal procedures
- From: Michael Trover
- Re: Pure and internal procedures
- From: Ian Harvey
- Re: Pure and internal procedures
- From: glen herrmannsfeldt
- Re: Pure and internal procedures
- From: Michael Trover
- Re: Pure and internal procedures
- From: Dick Hendrickson
- Re: Pure and internal procedures
- From: Michael Trover
- Re: Pure and internal procedures
- From: Dick Hendrickson
- Re: Pure and internal procedures
- From: Michael Trover
- Re: Pure and internal procedures
- From: Richard Maine
- Re: Pure and internal procedures
- From: Michael Trover
- Re: Pure and internal procedures
- From: glen herrmannsfeldt
- Re: Pure and internal procedures
- From: Michael Trover
- Pure and internal procedures
- Prev by Date: Re: Using ARPACK
- Next by Date: Re: Using ARPACK
- Previous by thread: Re: Pure and internal procedures
- Next by thread: Re: Pure and internal procedures
- Index(es):
Relevant Pages
|