Post on 13-Dec-2015
transcript
Week 4:Organizational matters
Register yourself to CIMS (Course Information and Management System)by clicking on Homework athidiv.cc.itu.edu.tr/~F90
Do not send your homeworks to me by e-mail!
Deadline for HW1 is postponed toMarch 10
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
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
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
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
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
Choice and decision-making
If criterion 1 thenaction 1
but if criterion 2 thenaction 2
but if criterion 3 thenaction 3
otherwiseaction 4
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
Logical expressions
Logical variables + logical constants + logical operators
Two values: true or false
logical variables
logical :: var_1, var_2, … Logical valued functions
function name(arg1, …) result logical_variable
logical :: logical_variable
Logical operators
L1 L2 L1 .or. L2 L1 .and. L2
true true true true
true false true false
false true true false
false false false false
L1 .not. L1
true false
false true
Logical operators
L1 L2 L1 .eqv. L2 L1 .neqv. L2
true true true false
true false false true
false true false true
false false true false
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
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
Comparing numbers
Accuracy/round-off– Number of significant digits for real
numbers Do not test whether two numbers are
equal Test whether their difference is
acceptably small
The case construct
select case (case_expression)
case (case_selector)
block_of_statements
...
case default
block_of_statements
end select
The case construct Case expression:
– either integer or character expression 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