Week 3
Let's review!Fundamental data types
List-directed input/output
Fundamental types of numbers
Integers Whole numbers (positive/negative/zero) Examples:
1952
3456787890123
0
-2334567 Typical range on a 32-bit computer
-2 x 109 to +2 x 109
Fundamental types of numbers Reals
+/- xxx.yyyyy
xxx integer part
yyyyy fractional part A better representation:
Sign: +/- Mantissa: a fraction between 0.1 and 1.0 Exponent: x 10e
- 0.923456 x 10-6 or -0.923456e-6
real and integer variables
Variable declaration:type :: name
type :: name1, name2, … integer :: a, b, c real :: x, y, z
Arithmetic expressions
A combination of variables, constants, operators, parentheses…
(4*Pi*Radius * *2 + 1.2098)/2.3
Assignment
name = expression
replace the content of the variable name with the result of the expression
List-directed input and output
read *, var_1, var_2, … only variables!
print *, item_1, item_2, … variables, constants, expressions, …
Value separators: Comma (,) Space Slash (/) End-of-line
List-directed input and output
Two consecutive commas: a null value is read the value of the variable is not set to zero, simply
its value is not changed! If the terminating character is a slash then no
more dataitems are read; processing of the input statement is ended
Example
Character data A B C D E F G H I J K L M N O P Q R S T U W X Y Z
a b c d e f g h i j k l m n o p q r s t u w x y z0 1 2 3 4 5 6 7 8 9lj = + - * / ( ) , . ' : ! " % & ; < > ? $
Declaration:character (len=length) :: name1, name2, …
character (len=6) :: a, b, c
character (len=10*3):: a
character (len=10) :: b
Assignmenta = "What a lovely afternoon!" a will have the value of "What a lovely afternoon!ljljljljljlj"b = ab will have the value of "What a lov"
Character data Concatenation:
Operator // Example:
character (len=10) a, b, c
a = "James"
b = "Bond"
c = trim(a)//"ž"//trim(b)
c will have the value of "James Bond"
Named constants
type, parameter :: name1=constant_expression1, …
real, parameter :: pi=3.1415926, pi_by_2 = pi/2.0
integer, parameter :: max_lines = 200
Now, some new stuff...
Basic Building Blocks
Procedures
Divide and rule!
Main program Procedure 1
Procedure 2 Procedure 3
Programs and modules
Main program unit
program nameuse statements...Specification statements...Executable statements...
end program name
Programs and modules
Module program unit
module nameuse statements...Specification statements...
containsProcedure definitions...
end module name
Procedures
Divide and rule!
Main program Procedure 1
Procedure 2 Procedure 3
Procedures
Procedures - origin “Write your own” (homemade) Intrinsic (built-in, comes with F )
•sin(x), cos(x), abs(x), … Written by someone else (libraries)
Procedures (subprograms) - form Functions Subroutines
Procedures
The main program amd any subprogram need never be aware of the internal details of any other program or subprogram!
Main program
Procedure 1
Procedure 2Procedure 3
Functionsfunction cube_root result(root)! A function to calculate the cube root of a positive
real number! Dummy argument declaration
real, intent(in) :: x! Result variable declaration
real :: root! Local variable declaration
real :: log_x! Calculate cube root by using logs
log_x = log(x)root = exp(log_x/3.0)
end function cube_root
Functions
function name (d1, d2, …) result(result_name)
Specifications part
Execution part end function name Variables
Internal (local) variables Result variable (keyword result) Dummy argument (keyword intent(in))
attribute
Functions
No side effects!
Restrictions on the statements that can appear within a function
Subroutinessubroutine roots (x, square_root, cube_root, fourth_root, & fifth_root)! Subroutine to calculate various roots of positive real! Number supplied as the first argument, and return them in! the second to fifth arguments
! Dummy argument declarations real, intent(in) :: x real, intent(out) :: square_root, cube_root, & fourth_root, fifth_root ! Local variable declaration real :: log_x ! Calculate square root using intrinsic sqrt square_root = sqrt(x) ! Calculate other roots by using logs log_x = log(x) cube_root = exp(log_x/3.0) fourth_root = exp(log_x/4.0) fifth_root = exp(log_x/5.0)
end subroutine roots
Subroutines
call name (arg1, arg2, …) intent(in), intent(out), intent(inout)
Arguments
Actual arguments in the calling program Dummy arguments in the subroutine or
function The order and types of the actual
arguments must correspond exactly with the order and types of the corresponding dummy arguments
Objects
Local variables vs. global variables private vs. public objects
Saving the values of local objects
Local entities within a procedure are not accessible from outside that procedure
Once an exit has been made, they cease to exist
If you want their values to ‘survive’ between calls, usereal, save :: list of real variables
Homework
Due on March 8 from Ellis & Philips
4.3 4.8 4.9 4.11