Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions source/learn/quickstart/organising_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,37 @@ use my_mod, only: printMat=>print_matrix
```

> Each module should be written in a separate `.f90` source file. Modules need to be compiled prior to any program units that `use` them.

## Optional arguments

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.

```
module norm_mod
implicit none
contains
function vector_norm(vec,p) result(norm)
real, intent(in) :: vec(:)
integer, intent(in), optional :: p ! power
real :: norm
if (present(p)) then ! compute Lp norm
norm = sum(abs(vec)**p) ** (1.0/p)
else ! compute L2 norm
norm = sqrt(sum(vec**2))
end if
end function vector_norm
end module norm_mod

program run_fcn
use norm_mod
implicit none

real :: v(9)

v(:) = 9

print *, 'Vector norm = ', vector_norm(v), vector_norm(v,2)
print *, 'L1 norm = ', vector_norm(v,1)

end program run_fcn
```
24 changes: 10 additions & 14 deletions source/learn/quickstart/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ the variable type and any other variable attributes.
The syntax for declaring variables is:

```
<variable_type> :: <variable_name>
<variable_type> :: <variable_name>, <variable_name>, ...
```

where `<variable_type>` is one of the built-in variable types listed above and
Expand All @@ -39,7 +39,7 @@ program variables
implicit none

integer :: amount
real :: pi
real :: pi, e ! two `real` variables declared
complex :: frequency
character :: initial
logical :: isOkay
Expand Down Expand Up @@ -104,16 +104,16 @@ In a similar way, we can read values from the command window
using the `read` statement:

```{play-code-block} fortran
program read_value
program read_values
implicit none
integer :: age
real :: x, y

print *, 'Please enter your age: '
read(*,*) age
print *, 'Please enter two numbers. '
read(*,*) x, y

print *, 'In ten years, your age will be ', age + 10
print *, 'The sum and product of the numbers are ', x+y, x*y

end program read_value
end program read_values
```

This input source is commonly referred to as `standard input` or `stdin`.
Expand All @@ -138,11 +138,7 @@ The usual set of arithmetic operators are available, listed in order of preceden
program arithmetic
implicit none

real :: pi
real :: radius
real :: height
real :: area
real :: volume
real :: pi, radius, height, area, volume

pi = 3.1415927

Expand Down Expand Up @@ -180,7 +176,7 @@ program float

float32 = 1.0_sp ! Explicit suffix for literal constants
float64 = 1.0_dp

print*, float32, float64
end program float
```

Expand Down