From 726ff138eccc3cbdc4526c18f5e34493bb35f375 Mon Sep 17 00:00:00 2001 From: Beliavsky <38887928+Beliavsky@users.noreply.github.com> Date: Sat, 9 Nov 2024 07:20:40 -0500 Subject: [PATCH] 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 --------- Co-authored-by: Henil Panchal Co-authored-by: Federico Perini --- source/learn/quickstart/organising_code.md | 34 ++++++++++++++++++++++ source/learn/quickstart/variables.md | 24 +++++++-------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/source/learn/quickstart/organising_code.md b/source/learn/quickstart/organising_code.md index 0f1b68188b7e..f3e8333dc958 100644 --- a/source/learn/quickstart/organising_code.md +++ b/source/learn/quickstart/organising_code.md @@ -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 +``` diff --git a/source/learn/quickstart/variables.md b/source/learn/quickstart/variables.md index 3007e2b09bed..c59126c24c96 100644 --- a/source/learn/quickstart/variables.md +++ b/source/learn/quickstart/variables.md @@ -23,7 +23,7 @@ the variable type and any other variable attributes. The syntax for declaring variables is: ``` - :: + :: , , ... ``` where `` is one of the built-in variable types listed above and @@ -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 @@ -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`. @@ -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 @@ -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 ```