Download presentation
Presentation is loading. Please wait.
Published byBelinda Greene Modified over 9 years ago
1
Week 4: Organizational matters Register yourself to CIMS (Course Information and Management System) by clicking on Homework at hidiv.cc.itu.edu.tr/~F90 n Do not send your homeworks to me by e- mail! n Deadline for HW1 is postponed to March 10
2
Week 4 Let’s start with a review
3
Procedures n Divide and rule! Main program Procedure 1 Procedure 2 Procedure 3
4
Programs and modules n Main program unit program name use statements. Specification statements. Executable statements. end program name
5
Programs and modules n Module program unit module name use statements. Specification statements. contains Procedure definitions. end module name
6
Procedures n Divide and rule! Main program Procedure 1 Procedure 2 Procedure 3
7
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
8
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
9
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
10
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
11
Functions n No side effects! Restrictions on the statements that can appear within a function
12
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
13
Subroutines call name (arg1, arg2, …) n intent(in), intent(out), intent(inout)
14
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
15
Objects n Local variables vs. global variables private vs. public objects
16
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
17
New stuff... Design Making Flow Control
18
Choice and decision-making If criterion 1 then action 1 but if criterion 2 then action 2 but if criterion 3 then action 3 otherwise action 4
19
Choice and decision-making if (criterion_1) then action_1 else if (criterion_2) then action_2 else if (criterion_3) then action_3 else action_4 endif
20
Logical expressions n Logical variables + logical constants + logical operators n Two values: true or false
21
logical variables n logical :: var_1, var_2, … n Logical valued functions function name(arg1, …) result logical_variable logical :: logical_variable
22
Logical operators L1L2L1.or. L2L1.and. L2 true true truefalsetruefalse falsetruetruefalse falsefalsefalsefalse L1.not. L1 truefalse falsetrue
23
Logical operators L1L2L1.eqv. L2L1.neqv. L2 true true true false truefalsefalsetrue falsetruefalsetrue falsefalsetruefalse
24
Relational operators a < b less than a <= b less than or equal to a > b greater than a >= b greater than or equal a == b equal a /= b not equal
25
Examples n (a d) n (x<=y).and. (y<=z) n.not. (a<b).eqv. (x<y) n a<b.neqv. x<y
26
Logical operator priorities n OperatorPriority.not. highest.and..or..eqv. and.neqv. lowest
27
The if construct if (logical expression) then block of F statements else if (logical expression) then block of F statements else if (logical expression) then block of F statements else block of F statements endif
28
Simple if construct if (logical expression) then block of F statements endif
29
Comparing numbers n Accuracy/round-off –Number of significant digits for real numbers n Do not test whether two numbers are equal n Test whether their difference is acceptably small
30
The case construct select case (case_expression) case (case_selector) block_of_statements... case default block_of_statements end select
31
The case construct n Case expression: –either integer or character expression n Case selector: –case_value case_expression = = case_value –low_value: low_value <= case_expression –:high_value case_expression <= high_value –low_value:high_value low_value <= case_expression.and. case_expression <= high_value
32
Homework n Due on March 15 n from Ellis & Philips –4.11 –5.11
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.