Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 3 Let's review! Fundamental data types List-directed input/output.

Similar presentations


Presentation on theme: "Week 3 Let's review! Fundamental data types List-directed input/output."— Presentation transcript:

1 Week 3 Let's review! Fundamental data types List-directed input/output

2 Fundamental types of numbers n Integers  Whole numbers (positive/negative/zero)  Examples: 1952 3456787890123 0 -2334567  Typical range on a 32-bit computer -2 x 10 9 to +2 x 10 9

3 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 - 0.923456 x 10 -6 or -0.923456e-6

4 real and integer variables n Variable declaration: type :: name type :: name1, name2, … n integer :: a, b, c n real :: x, y, z

5 Arithmetic expressions n A combination of variables, constants, operators, parentheses… (4*Pi*Radius * *2 + 1.2098)/2.3

6 Assignment name = expression replace the content of the variable name with the result of the expression

7 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

8 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

9 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 0 1 2 3 4 5 6 7 8 9 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"

10 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"

11 Named constants type, parameter :: name1=constant_expression1, … real, parameter :: pi=3.1415926, pi_by_2 = pi/2.0 integer, parameter :: max_lines = 200

12 Now, some new stuff... Basic Building Blocks

13 Procedures n Divide and rule! Main program Procedure 1 Procedure 2 Procedure 3

14 Programs and modules n Main program unit program name use statements. Specification statements. Executable statements. end program name

15 Programs and modules n Module program unit module name use statements. Specification statements. contains Procedure definitions. end module name

16 Procedures n Divide and rule! Main program Procedure 1 Procedure 2 Procedure 3

17 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

18 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

19 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

20 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

21 Functions n No side effects! Restrictions on the statements that can appear within a function

22 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

23 Subroutines call name (arg1, arg2, …) n intent(in), intent(out), intent(inout)

24 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

25 Objects n Local variables vs. global variables private vs. public objects

26 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

27 Homework n Due on March 8 n from Ellis & Philips  4.3  4.8  4.9  4.11


Download ppt "Week 3 Let's review! Fundamental data types List-directed input/output."

Similar presentations


Ads by Google