Skip to content

Commit 726ff13

Browse files
Beliavskyhenilp105perazz
authored
Update organising_code.md to cover optional arguments (#466)
* Update organising_code.md to cover optional arguments * fixed typo in organising_code.md * Update variables.md Show declaration of multiple variables in one line, gives an example of reading more than one variable in one statement. * Updated code in variables.md with a comment * Updated program float in variables.md Added a print statement so that the user sees that a double precision is stored to more digits than a single precision one. * removed unused variable n in vector_norm * declared norm in vector_norm * Update source/learn/quickstart/variables.md Co-authored-by: Federico Perini <[email protected]> --------- Co-authored-by: Henil Panchal <[email protected]> Co-authored-by: Federico Perini <[email protected]>
1 parent 136ca15 commit 726ff13

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

source/learn/quickstart/organising_code.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,37 @@ use my_mod, only: printMat=>print_matrix
171171
```
172172

173173
> Each module should be written in a separate `.f90` source file. Modules need to be compiled prior to any program units that `use` them.
174+
175+
## Optional arguments
176+
177+
An advantage of placing subroutines and functions in modules is that they can have ```optional``` arguments. In a procedure with an argument declared optional, the ```present``` function is used to test if the argument was set in the caller. Optional arguments that are not present may not be accessed within the procedure. Here is a generalization of the ```vector_norm``` function that can use powers other than 2 to compute the Lp norm.
178+
179+
```
180+
module norm_mod
181+
implicit none
182+
contains
183+
function vector_norm(vec,p) result(norm)
184+
real, intent(in) :: vec(:)
185+
integer, intent(in), optional :: p ! power
186+
real :: norm
187+
if (present(p)) then ! compute Lp norm
188+
norm = sum(abs(vec)**p) ** (1.0/p)
189+
else ! compute L2 norm
190+
norm = sqrt(sum(vec**2))
191+
end if
192+
end function vector_norm
193+
end module norm_mod
194+
195+
program run_fcn
196+
use norm_mod
197+
implicit none
198+
199+
real :: v(9)
200+
201+
v(:) = 9
202+
203+
print *, 'Vector norm = ', vector_norm(v), vector_norm(v,2)
204+
print *, 'L1 norm = ', vector_norm(v,1)
205+
206+
end program run_fcn
207+
```

source/learn/quickstart/variables.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ the variable type and any other variable attributes.
2323
The syntax for declaring variables is:
2424

2525
```
26-
<variable_type> :: <variable_name>
26+
<variable_type> :: <variable_name>, <variable_name>, ...
2727
```
2828

2929
where `<variable_type>` is one of the built-in variable types listed above and
@@ -39,7 +39,7 @@ program variables
3939
implicit none
4040
4141
integer :: amount
42-
real :: pi
42+
real :: pi, e ! two `real` variables declared
4343
complex :: frequency
4444
character :: initial
4545
logical :: isOkay
@@ -104,16 +104,16 @@ In a similar way, we can read values from the command window
104104
using the `read` statement:
105105

106106
```{play-code-block} fortran
107-
program read_value
107+
program read_values
108108
implicit none
109-
integer :: age
109+
real :: x, y
110110
111-
print *, 'Please enter your age: '
112-
read(*,*) age
111+
print *, 'Please enter two numbers. '
112+
read(*,*) x, y
113113
114-
print *, 'In ten years, your age will be ', age + 10
114+
print *, 'The sum and product of the numbers are ', x+y, x*y
115115
116-
end program read_value
116+
end program read_values
117117
```
118118

119119
This input source is commonly referred to as `standard input` or `stdin`.
@@ -138,11 +138,7 @@ The usual set of arithmetic operators are available, listed in order of preceden
138138
program arithmetic
139139
implicit none
140140
141-
real :: pi
142-
real :: radius
143-
real :: height
144-
real :: area
145-
real :: volume
141+
real :: pi, radius, height, area, volume
146142
147143
pi = 3.1415927
148144
@@ -180,7 +176,7 @@ program float
180176
181177
float32 = 1.0_sp ! Explicit suffix for literal constants
182178
float64 = 1.0_dp
183-
179+
print*, float32, float64
184180
end program float
185181
```
186182

0 commit comments

Comments
 (0)