-
Notifications
You must be signed in to change notification settings - Fork 0
/
overload.f90
80 lines (65 loc) · 1.82 KB
/
overload.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
! Uage of interface in FORTRAN : Operator overloading
! function newFunc(x,x1) result(y)
! real, intent(in) :: x,x1
! real :: y
! y=sqrt(x**2+x1**2)
! end function newFunc
! program myProgram
! implicit none
! real :: a,b,c
! interface operator(*) ! this means call * on two real value to return norm, ---> operator collision
! real function newFunc(x,x1) result (y)
! real, intent(in) :: x,x1
! end function newFunc
! end interface operator(*)
! a=3.0
! b=4.0
! c=newFunc(a,b)
! print *,c
! end program myProgram
! function newFunc(x,x1) result(y)
! real, intent(in) :: x,x1
! real :: y
! y=sqrt(x**2+x1**2)
! end function newFunc
! program myProgram
! implicit none
! real :: a,b,c
! interface operator(.no.) ! so we define a new operator that does not collide with other and works
! real function newFunc(x,x1) result (y)
! real, intent(in) :: x,x1
! end function newFunc
! end interface operator(.no.)
! a=3.0
! b=4.0
! c=a.no.b
! print *,c
! end program myProgram
! a new type that hides the real type, because modified * can't work on intrinsic real type
module newT
type myreal
real x
end type myreal
end module newT
function newFunc(x,x1) result(y)
use newT
type(myreal), intent(in) :: x,x1
real :: y
y=sqrt(x%x**2+x1%x**2) ! takes `x` argument of myreal and calculate norm
end function newFunc
program myProgram
use newT
implicit none
type(myreal) :: a,b
real c
interface operator(*) ! the * operator now operates over the myreal type
real function newFunc(x,x1) result (y)
use newT
type(myreal), intent(in) :: x,x1
end function newFunc
end interface operator(*)
a%x=3.0
b%x=4.0
c=a*b! the * operator now operates over the myreal type
print *,c
end program myProgram