fortran. fortran or formula translation was developed in the 1950's by ibm as an alternative...

15
FORTRAN

Upload: matthew-mills

Post on 21-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

FORTRAN

Page 2: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

Fortran or FORmula TRANslat ion was developed in the 1950's by IBM as an a l ternat ive to Assembly Language. Fi rs t successful l h igh level language.

FORTRAN 66 First HLL Standard

FORTRAN 77 Character data types Improved Do Loops and If statements

Fortran 90 Free format source code Modern control structures Derived Data Types Powerful array notation Dynamic Memory allocation Operator overloading Keyword argument passing The INTENT attribute Control of numeric precision Support for Modules

Fortran 95 minor improvements

Fortran 2003 Object oriented Fortran

Fortran 2008 Concurrent Programming

Fortran 2015 Minor improvements

BACKGROUND

Programming Languages Family Tree

Page 3: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

You wi l l want to be comfortable using an text-editor l ike v i or emacs – that supports syntax highl ight ing and auto - indentat ion etc. . . There is an ide cal led Photran that I have never used.

I prefer emacs but both v i and emacs have s imi lar funct ional i ty. I usual ly run emacs in the terminal ( fast!)

emacs program.f90 –nw Important emacs commands to remember

ctrl-x ctrl-f (fi nd a fi le) ctrl-x ctrl-s (save current fi le) ctrl-x ctrl-c (close emacs) ctrl-s (search) ctrl-x 2 (split window horizontally) ctrl-x 3 (split window vertically) ctrl-x 0 (close current window) ctrl-x o (switch to next window) ctrl-x k (kill current fi le) ctrl-x b (switch to next fi le alt-% (search and replace) ctrl-a (start of line) ctrl-e (end of line)

Also good to remember 'ctr l -z ' to suspend emacs and ' fg ' to resume

DEVELOPMENT ENVIRONMENT

Page 4: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

Open a terminal in mac or linuxPutty or MobaxTerm in windowsssh <netid>@bluehive.circ.rochester.edumkdir fortran_classcd fortran_classemacs hello_world.f90

CONNECTING TO BLUEHIVE

Page 5: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

I will use intel compiler although gfortran compiler will also work. module load intel ifort hello_world.f90 ./a.out

COMPILING

Page 6: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

/publ ic / jcarrol5/fortran/ex1.f90 Program structure Comments (! This is a comment) Continuat ion &

l ines Variable Declarat ions Data Types

INTEGER REAL CHARACTER LOGICAL

Operators Arithmetic: +, -, /, *, ** CHARACTER concatenation: // Relational: >, <, >=, <=, /=, == Logical: .AND., .OR., .XOR., .NOT., .EQV., .NEQV. Use () to group expressions

Intr ins ic numerical funct ions MOD, FLOOR, CEILING, SIGN, MAX, MIN

Intr ins ic math funct ions SQRT, LOG, LOG10, EXP, SIN, COS, TAN, SINH, COSH, TANH, ASIN, ACOS, ATAN

Basic I /O READ, WRITE

FORTRAN BASICS

Page 7: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

Exercise 1: Write a program that uses Heron's formula to calculate the area of a triangle given the length of its three sides.

EXERCISE 1

Page 8: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

/public/jcarrol5/fortran/ex1b.f90Format specifi ers.

write(*,'(A10,2F12.5,I3,L1)') string1, float1, float2, int1, logical1

I – integer A – string F – float E – exponential EN – scientific notation ES – engineering notation

All format specifi ers can be modifi ed with a repeat count and width. In addition, the precision can be specifi ed for Format specifi ers for real numbers. 4F10.5 means use a floating point format of width 10 and 5

decimal points 4 times.

FORMAT SPECIFIERS

Page 9: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

/public/jcarrol5/fortran/ex2.f90Fortran programs can contain subroutines and

functions. Functions return a value Subroutines do not

Fortran normally passes variables by reference – so be careful when modifying function or subroutine arguments. Best to declare the intent to avoid common mistakes.

If a constant or an expression is passed as an argument, a temporary variable is created to store the result of the expression before being passed to the subroutine or function.

SUBROUTINES AND FUNCTIONS

Page 10: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

Modify previous program to use a function to calculate the area of the triangle.

Use format specifiers for the output.

EXERCISE 2

Page 11: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

/public/jcarrol5/fortran/ex3.f90

Fortran has 3 basic control structures DO loops

With a counter (an optional increment) DO counter=start, end[, increment]

... END DO

With a while statement DO WHILE (a < b)

... END DO

With an exit statement DO

... IF (condition) EXIT

END DO

IF blocks IF (condition) THEN

... ELSE IF (other condition) THEN

... ELSE

... END IF

SELECT CASE statements SELECT CASE (var) CASE(constant1)

execution statements CASE(constant2)

execution statements CASE DEFAULT

execution statements END SELECT

CONTROL STRUCTURES

Page 12: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

Write a Fortran program to calculate the number of uniq combinations of size r taken from a set of size n – ie 'n choose r'.

Validate that n and r are both non-negative and that n is greater than or equal to r.

Write a factorial function Write a combination function that calls the factorial

function Format the output

EXERCISE 3

Page 13: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

/public/jcarrol5/fortran/ex4.f90Often a subroutine or function may want to call itself.

This is called recursion. Recursive subroutines and functions require the

recursive keyword.Need to have some termination condition

RECURSIVE FUNCTIONS AND SUBROUTINES

Page 14: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

Modify the factorial function in your combination program to be recursive.

EXERCISE 4

Page 15: FORTRAN.  Fortran or FORmula TRANslation was developed in the 1950's by IBM as an alternative to Assembly Language. First successfull high level language

ModulesDerived Data TypesPublic, Private EntitiesArraysNamelistsReading/Writing to fi lesPointers

TOMORROW