Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing.

Slides:



Advertisements
Similar presentations
C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
Advertisements

Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Input and Output READ WRITE OPEN. FORMAT statement Format statements allow you to control how data are read or written. Some simple examples: Int=2; real=
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
FORTRAN Short Course Week 1 Kate T-C February 17, 2008.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Introduction to Computing Science and Programming I
Introduction to Unix – CS 21 Lecture 11. Lecture Overview Shell Programming Variable Discussion Command line parameters Arithmetic Discussion Control.
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
Chapter 6: Functions.
Functions. Program complexity the more complicated our programs get, the more difficult they are to develop and debug. It is easier to write short algorithms.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Subroutines Just like C, PERL offers the ability to use subroutines for all the same reasons – Code that you will use over and over again – Breaking large.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Homework Reading Programming Assignments
Introduction to FORTRAN
Beginning Fortran Fortran (77) Basics 22 October 2009 *Black text on white background provided for easy printing.
Guidelines for the CMM coding project 5 October 2006 (or, “How to make your life easier in the long run”)
Fortran 1- Basics Chapters 1-2 in your Fortran book.
8 Shell Programming Mauro Jaskelioff. Introduction Environment variables –How to use and assign them –Your PATH variable Introduction to shell programming.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Introduction to Python
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
David Stotts Computer Science Department UNC Chapel Hill.
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
Python Functions.
1 CS161 Introduction to Computer Science Topic #9.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Conditional Statements.  Quiz  Hand in your jQuery exercises from last lecture  They don't have to be 100% perfect to get full credit  They do have.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
FUNCTIONS IN FORTRAN For a complex program difficulties like writing it and debugging are encountered. These can be minimized by breaking main program.
Procedures Subs and Functions. Procedures Before OOP, subroutines were the primary high-level way to organize a program. In OOP, this role has been taken.
Python Let’s get started!.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Beginning Fortran Introduction 13 October 2009 *Black text on white background provided for easy printing.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
PHP Form Processing * referenced from
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
AVCE ICT – Unit 7 - Programming Session 12 - Debugging.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Alice and Java Unit 7 1. Day 1  Objective: Gain an introduction to Java and Eclipse  Essential skill: DM-1: Use technology to advance critical thinking.
C++ Functions A bit of review (things we’ve covered so far)
 Prepared by: Eng. Maryam Adel Abdel-Hady
BIT116: Scripting Lecture 05
User-Written Functions
Learning to Program D is for Digital.
Python Let’s get started!.
Introduction to Python
COMP 170 – Introduction to Object Oriented Programming
Character (String) Data
Pre-processor Directives
What is Bash Shell Scripting?
Number and String Operations
Engineering Computation with MATLAB
Coding Concepts (Basics)
The structure of programming
A First Program.
Arrays.
CPS125.
Presentation transcript:

Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing

Example Code Write a program to read in five values of temperature in Fahrenheit and convert to degrees Celsius OR Kelvin OR both.

Your Typical Program c PROGRAM MYPROGRAM STOP END Program Options Declaration of Variables MAIN CODE

Program Start Options/Variable Declaration Main Code Program End c PROGRAM CONVERTF IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I DO I = 1, NT READ(*,*) F(I) ENDDO WRITE(*,*) ‘Convert to C?’ READ(*,*) DOC WRITE(*,*) ‘Convert to K?’ READ(*,*) DOK DO I = 1, NT C(I) = (5./9.)*(F(I)-32.) ENDDO IF (DOK.EQV..TRUE.) THEN DO I = 1, NT K(I) = C(I) ENDDO ENDIF IF ((DOC.EQV..TRUE.).AND. (DOK.EQV..FALSE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, C(I), ‘C’ ENDDO ENDIF c IF ((DOC.EQV..FALSE.).AND. (DOK.EQV..TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, K(I), ‘K’ ENDDO ENDIF c IF ((DOC.EQV..TRUE.).AND. (DOK.EQV..TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, C(I), ‘C ’, K(I), ‘K’ ENDDO ENDIF STOP END

c PROGRAM CONVERTF IMPLICIT NONE PARAMETER NT = 5 REAL F(NT) REAL K(NT) REAL C(NT) LOGICAL DOC LOGICAL DOK INTEGER I DO I = 1, NT READ(*,*) F(I) ENDDO WRITE(*,*) ‘Convert to C?’ READ(*,*) DOC WRITE(*,*) ‘Convert to K?’ READ(*,*) DOK DO I = 1, NT C(I) = (5./9.)*(F(I)-32.) ENDDO IF (DOK.EQV..TRUE.) THEN DO I = 1, NT K(I) = C(I) ENDDO ENDIF IF ((DOC.EQV..TRUE.).AND. (DOK.EQV..FALSE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, C(I), ‘C’ ENDDO ENDIF c IF ((DOC.EQV..FALSE.).AND. (DOK.EQV..TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, K(I), ‘K’ ENDDO ENDIF c IF ((DOC.EQV..TRUE.).AND. (DOK.EQV..TRUE.)) THEN DO I = 1, NT WRITE(*,*) F(I), ‘F = ‘, C(I), ‘C ’, K(I), ‘K’ ENDDO ENDIF STOP END This program is not particularly useful: -What if we want to calculate 3 temperatures? Or 30? -Open source code, change NT, recompile, re-run … -We are primarily restricted because of the necessity of declaring the value of NT: it is a parameter that has to be set in the code before anything else is done.

Subroutines Mini-programs that are run inside of a larger program (or subroutine): Main Program Code CALL SUBROUTINE(a,b,c) Code STOP END INTEGER a, b, c

Subroutines Subroutine is called, with variables a, b, and c passed to it. Those same variables, at their current values, now exist in subroutine. Main Program Code CALL SUBROUTINE(a,b,c) Code STOP END INTEGER a, b, c Subroutine(a,b,c) INTEGER a, b, c

Subroutines Subroutine is called, with variables a, b, and c passed to it. Those same variables, at their current values, now exist in subroutine. Main Program Code CALL SUBROUTINE(a,b,c) Code STOP END INTEGER a, b, c Subroutine(a,b,c) INTEGER a, b, c

Subroutines Additional variables can be declared within the subroutine. These exist only in the subroutine, and will be destroyed when the routine is done. Main Program Code CALL SUBROUTINE(a,b,c) Code STOP END INTEGER a, b, c Subroutine(a,b,c) INTEGER a, b, c INTEGER d, e, f

Subroutines Lines of code manipulating variables are processed in the subroutine, possibly changing the values of a, b, and c. Main Program Code CALL SUBROUTINE(a,b,c) Code STOP END INTEGER a, b, c Subroutine(a,b,c) INTEGER a, b, c INTEGER d, e, f Code

Subroutines At RETURN, code returns to main program after subroutine was called. Variables a, b, and c now have NEW values depending on subroutine. Main Program Code CALL SUBROUTINE(a,b,c) Code STOP END INTEGER a, b, c Subroutine(a,b,c) INTEGER a, b, c INTEGER d, e, f Code RETURN END

Subroutines Main program continues on after the subroutine call with new values for a, b, and c. Main Program Code CALL SUBROUTINE(a,b,c) Code STOP END INTEGER a, b, c Subroutine(a,b,c) INTEGER a, b, c INTEGER d, e, f Code RETURN END

Subroutines When calling a subroutine, the main program is basically saying: “Given these variables at their current values, do some stuff and then give me my variables back. Don’t worry; I’ll wait.”

Subroutines When calling a subroutine, the main program is basically saying: “Given these variables at their current values, do some stuff and then give me my variables back. Don’t worry; I’ll wait.” CALL (arg1,arg2,arg3,…) “input arguments”

Input Arguments Any variables that the main program wants the subroutine to make use of have to be passed to the subroutine as input arguments. These appear as a list of the names of the variables in parentheses after the name of the subroutine being called. CALL AVERAGE(X1,X2,AVG)

Input Arguments Any variables that the main program wants the subroutine to make use of have to be passed to the subroutine as input arguments. These appear as a list of the names of the variables in parentheses after the name of the subroutine being called. CALL AVERAGE(X1,X2,AVG) Subroutine AVERAGE takes in three variables: two numbers (X1 and X2) and a variable that will represent the average of them (AVG).

Input Arguments In the subroutine, these variables are re- declared so that they now exist simultaneously in the main program and in the subroutine: C SUBROUTINE AVERAGE(X1,X2,AVG) IMPLICIT NONE REAL X1 REAL X2 REAL AVG

Input Arguments In the subroutine, these variables are re- declared so that they now exist simultaneously in the main program and in the subroutine: C SUBROUTINE AVERAGE(X1,X2,AVG) IMPLICIT NONE REAL X1 REAL X2 REAL AVG NOTE: These variables must be declared as the same type as they are declared in the main program.

Input Arguments Now we can write code in the subroutine to compute AVG from X1 and X2: C SUBROUTINE AVERAGE(X1,X2,AVG) IMPLICIT NONE REAL X1 REAL X2 REAL AVG c AVG = (X1 + X2)/2. RETURN END

Input Arguments Any of the input arguments that were changed in the subroutine will now have new values in the main program. X1 = 3.0 X2 = 8.0 AVG = CALL AVERAGE(X1,X2,AVG) WRITE(*,*) AVG AVG = (X1 + X2)/2. RETURN END Main Program AVERAGE(X1,X2,AVG)

Input Arguments Any of the input arguments that were changed in the subroutine will now have new values in the main program. X1 = 3.0 X2 = 8.0 AVG = CALL AVERAGE(X1,X2,AVG) WRITE(*,*) AVG AVG = (X1 + X2)/2. RETURN END Main Program AVERAGE(X1,X2,AVG) AVG is sent to subroutine with value of

Input Arguments Any of the input arguments that were changed in the subroutine will now have new values in the main program. X1 = 3.0 X2 = 8.0 AVG = CALL AVERAGE(X1,X2,AVG) WRITE(*,*) AVG AVG = (X1 + X2)/2. RETURN END Main Program AVERAGE(X1,X2,AVG) Subroutine changes value of AVG to 5.5

Input Arguments Any of the input arguments that were changed in the subroutine will now have new values in the main program. X1 = 3.0 X2 = 8.0 AVG = CALL AVERAGE(X1,X2,AVG) WRITE(*,*) AVG AVG = (X1 + X2)/2. RETURN END Main Program AVERAGE(X1,X2,AVG) At RETURN, we return to the main program, where AVG now has a value of 5.5

Passing Arrays You can also pass arrays to a subroutine: – Pass the array dimensions as INTEGERs first, then pass the array itself PROGRAM MAIN PARAMETER NX = 360 PARAMETER NY = 181 REAL TEMPS(NX,NY) CALL ZILCH(NX,NY,TEMPS) STOP END SUBROUTINE ZILCH(NX,NY,VAR) INTEGER NX INTEGER NY REAL VAR(NX,NY) INTEGER I INTEGER J DO J = 1, NY DO I = 1, NX VAR(I,J) = 0. ENDDO RETURN END

Passing Arrays Notice how the same variable can have different names in the main program and the subroutine. PROGRAM MAIN PARAMETER NX = 360 PARAMETER NY = 181 REAL TEMPS(NX,NY) CALL ZILCH(NX,NY,TEMPS) STOP END SUBROUTINE ZILCH(NX,NY,VAR) INTEGER NX INTEGER NY REAL VAR(NX,NY) INTEGER I INTEGER J DO J = 1, NY DO I = 1, NX VAR(I,J) = 0. ENDDO RETURN END

Passing Arrays SUBROUTINE ZILCH(NX,NY,VAR) INTEGER NX INTEGER NY REAL VAR(NX,NY) INTEGER I INTEGER J DO J = 1, NY DO I = 1, NX VAR(I,J) = 0. ENDDO RETURN END Notice something interesting here: -We took in values from the main program for NX and NY, then created a variable VAR based on those dimensions. -We can do this, because input arguments are special

Rules of Subroutines Subroutines are basically small programs built inside of a larger program Input arguments are handed down to the subroutine from its parent program – the subroutine is constructed with a priori knowledge of these variables and their present value “Given a, b, and c, do something and then give me a, b, and c back.”

Rules of Subroutines Since the subroutine is built with the input arguments already known, we can make an array inside of the subroutine based on variables passed into it. PROGRAM MAIN INTEGER NX INTEGER NY READ(*,*) NX, NY CALL MAKEARRAY(NX,NY) STOP END SUBROUTINE MAKEARRAY(NX,NY) INTEGER NX INTEGER NY REAL VAR(NX,NY) … RETURN END

Rules of Subroutines Since the subroutine is built with the input arguments already known, we can make an array inside of the subroutine based on variables passed into it. PROGRAM MAIN INTEGER NX INTEGER NY READ(*,*) NX, NY CALL MAKEARRAY(NX,NY) STOP END SUBROUTINE MAKEARRAY(NX,NY) INTEGER NX INTEGER NY REAL VAR(NX,NY) … RETURN END NX, NY read from user into MAINNX, NY handed to MAKEARRAY to make VAR

Rules of Subroutines This is something we couldn’t do before – CONVERTF was not particularly useful because we couldn’t ask the user for the dimensions of array “F” and then make an array, we had to use a parameter. However, we can ask for the dimensions of the array and create an array inside of a subroutine with those dimensions, because for the subroutine, that variable is passed in as a priori knowledge.

CONVERTF VARIABLE DECLARATION: PARAMETER NX = 5 VARIABLE DECLARATION: PARAMETER NX = 5 MAIN CODE: STOP END MAIN CODE: STOP END CONVERTF2 VARIABLE DECLARATION: INTEGER NX VARIABLE DECLARATION: INTEGER NX MAIN CODE: CALL SUB(NX) STOP END MAIN CODE: CALL SUB(NX) STOP END SUB(NX) VARIABLE DECLARATION: INTEGER NX VARIABLE DECLARATION: INTEGER NX MAIN CODE: RETURN END MAIN CODE: RETURN END

Using Subroutines You can write subroutines in two different ways: – You can write the subroutine directly within the parent program – You can write the subroutine in a separate *.f file and compile it along with the parent program

Using Subroutines … STOP END SUBROUTINE MYSUBROUTINE(X1,X2,X3,…) IMPLICIT NONE REAL X1 REAL X2 …

Compiling With Subroutines Compile external subroutines separately using the following syntax: pgf77 –c.f Then compile the parent program with the subroutine *.o files attached: pgf77 … -o.f

Proper Coding Technique There are some simple rules to follow to make sure that your code is readable and is robust – you can do a lot with it without having to write new source code.

Write in CAPITAL LETTERS This isn’t required, but writing code in capital letters and comments in lower-case makes it easier to distinguish the two from each other.

Use Subroutines Often If you find yourself having to code the same thing in over and over in your program, it is better to block that code off into a subroutine and call the subroutine every time you need to perform that function.

Proper Subroutine Usage Main Program Driver Subroutine Sub 1 Sub 4 Sub 2 Sub 3

Proper Subroutine Usage Main Program Driver Subroutine Sub 1 Sub 4 Sub 2 Sub 3 Main Program essentially just reads in necessary information to begin program (array sizes, etc). Passes all of that information to the Driver Subroutine.

Proper Subroutine Usage Main Program Driver Subroutine Sub 1 Sub 4 Sub 2 Sub 3 Most of the main functions of the program are completed in the Driver Subroutine, where variables are declared based on information passed in from the Main Program.

Proper Subroutine Usage Main Program Driver Subroutine Sub 1 Sub 4 Sub 2 Sub 3 Individual functions that need to be completed (some multiple times) are written in different subroutines, which are called from the Driver Subroutine.

Proper Subroutine Usage Main Program Driver Subroutine Sub 1 Sub 4 Sub 2 Sub 3 You will find that most, if not all, professional code is written in this format.

Make Compilation Clear Programs can have complicated compilations, especially when you start attaching multiple subroutines with different options. If the compilation can be completed on a single line, put a comment in the code somewhere near the top containing the compilation line. If it’s more complex than that, make a script that compiles the program for you.

Use Source Directories Rather than keeping your source code, your subroutines, and your compiled programs all in the same directory, create a ‘source’ subdirectory where you keep your source code. This will prevent you from accidentally deleting, replacing, or otherwise ruining the code you’ve worked on.

Don’t Re-Invent The Wheel If you write your subroutines in separate files, you’ll find yourself using them in multiple programs – there’s no need to rewrite it. Use professional subroutine packages where possible – e.g. spherepack.