Week 3 Let's review! Fundamental data types List-directed input/output
Fundamental types of numbers n Integers Whole numbers (positive/negative/zero) Examples: Typical range on a 32-bit computer -2 x 10 9 to +2 x 10 9
Fundamental types of numbers n Reals +/- xxx.yyyyy xxx integer part yyyyy fractional part n A better representation: Sign:+/- Mantissa:a fraction between 0.1 and 1.0 Exponent:x 10 e x or e-6
real and integer variables n Variable declaration: type :: name type :: name1, name2, … n integer :: a, b, c n real :: x, y, z
Arithmetic expressions n A combination of variables, constants, operators, parentheses… (4*Pi*Radius * * )/2.3
Assignment name = expression replace the content of the variable name with the result of the expression
List-directed input and output n read *, var_1, var_2, … only variables! n print *, item_1, item_2, … variables, constants, expressions, … n Value separators: Comma (,) Space Slash (/) End-of-line
List-directed input and output n Two consecutive commas: a null value is read the value of the variable is not set to zero, simply its value is not changed! n If the terminating character is a slash then no more dataitems are read; processing of the input statement is ended n Example
Character data n 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 z lj = + - * / ( ),. ' : ! " % & ; ? $ Declaration: character (len=length) :: name1, name2, … character (len=6) :: a, b, c character (len=10*3):: a character (len=10) :: b Assignment a = "What a lovely afternoon!" a will have the value of "What a lovely afternoon! ljljljljljlj " b = a b will have the value of "What a lov"
Character data n Concatenation: Operator // n 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= , pi_by_2 = pi/2.0 integer, parameter :: max_lines = 200
Now, some new stuff... Basic Building Blocks
Procedures n Divide and rule! Main program Procedure 1 Procedure 2 Procedure 3
Programs and modules n Main program unit program name use statements. Specification statements. Executable statements. end program name
Programs and modules n Module program unit module name use statements. Specification statements. contains Procedure definitions. end module name
Procedures n Divide and rule! Main program Procedure 1 Procedure 2 Procedure 3
Procedures n Procedures - origin “Write your own” (homemade) Intrinsic (built-in, comes with F ) sin(x), cos(x), abs(x), … Written by someone else (libraries) n Procedures (subprograms) - form Functions Subroutines
Procedures n 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 2 Procedure 3
Functions function 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 n Variables Internal (local) variables Result variable (keyword result ) Dummy argument (keyword intent(in) ) attribute
Functions n No side effects! Restrictions on the statements that can appear within a function
Subroutines subroutine 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, …) n intent(in), intent(out), intent(inout)
Arguments n Actual arguments in the calling program n Dummy arguments in the subroutine or function n The order and types of the actual arguments must correspond exactly with the order and types of the corresponding dummy arguments
Objects n Local variables vs. global variables private vs. public objects
Saving the values of local objects n Local entities within a procedure are not accessible from outside that procedure n Once an exit has been made, they cease to exist n If you want their values to ‘survive’ between calls, use real, save :: list of real variables
Homework n Due on March 8 n from Ellis & Philips 4.3 4.8 4.9 4.11