Skip to content

Commit

Permalink
Update organising_code.md to cover optional arguments (#466)
Browse files Browse the repository at this point in the history
* 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 <federico.perini@gmail.com>

---------

Co-authored-by: Henil Panchal <henilp105@gmail.com>
Co-authored-by: Federico Perini <federico.perini@gmail.com>
  • Loading branch information
3 people authored Nov 9, 2024
1 parent 136ca15 commit 726ff13
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
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

0 comments on commit 726ff13

Please sign in to comment.