-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfunction.txt
37 lines (26 loc) · 1.03 KB
/
function.txt
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
Ways of defining a function:
1. Function and result name are the same.
2. RESULT has a different name within the function, which can be used
to return an array.
RETURN, when needed, is used to exit the function, not set its value.
Long version
============
There are two forms of syntax for defining a function in Fortran.
1. The function and result name can be the same, for example
integer function twice(i)
integer, intent(in) :: i
twice = 2*i
end function twice
2. The RESULT can have a different name than the function. This
syntax, which can be used to return an array, was introduced in
Fortran 90.
pure function twice(i) result(i2)
integer, intent(in) :: i
integer :: i2 ! declare the RESULT
i2 = 2*i
end function twice
Since the second syntax can be used for any function, I suggest using
it for all functions, for uniformity and since one syntax is easier to
remember.
The RETURN statement is used to exit the function, not set its value,
as in other languages. Often it is not needed.