forked from zziolko/fortran-class
-
Notifications
You must be signed in to change notification settings - Fork 0
/
function-as-an-argument.f90
56 lines (37 loc) · 1.01 KB
/
function-as-an-argument.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
program function_as_an_argument
implicit none
print *, 'Linear function'
call EvaluateRange(-1.0,1.0,0.2,LinearFunction)
print *, 'Quadratic function'
call EvaluateRange(-1.0,1.0,0.2,QuadraticFunction)
contains
! y = a * x
real function LinearFunction(a,x) result(y)
implicit none
real, intent(in) :: a, x
y = a * x
end function LinearFunction
! y = a * x^2
real function QuadraticFunction(a,x) result(y)
implicit none
real, intent(in) :: a, x
y = a * x**2
end function QuadraticFunction
! Evaluate function `func` on grid [x0,x1] with step dx
subroutine EvaluateRange(x0, x1, dx, my_func)
implicit none
real,intent(in) :: x0, x1, dx
real :: a, x
interface
real function my_func(a,x)
real, intent(in) :: a, x
end function my_func
end interface
a = 1.0
x = x0
do while (x <= x1)
print *,x, my_func(a,x)
x = x + dx
end do
end subroutine EvaluateRange
end program function_as_an_argument