mdul-cntns-sub-cmmnts2.f90

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Introduction to arrays
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.
Programming in Visual Basic
Chapter 2 Basic Elements of Fortan
Fortran 9x HTML version. New F90 features Free source form Modules User-defined data types and operators Generic user-defined procedures Interface blocks.
Chapter 10 Modules and programming with subroutines.
Guide To UNIX Using Linux Third Edition
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Fortran: Specification Statements Session Six ICoCSIS.
Introduction to FORTRAN
IE 212: Computational Methods for Industrial Engineering
16&17-2 Grasp the concept of top-down programming Identify Function Headers and Prototypes Understand when and where prototypes used Understand how arguments.
Multi-Dimensional Arrays
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
FORTRAN FORmula TRANslator -Anand Trivedi. HISTORY  Designed and written from scratch in by an IBM team lead by John W. Backus as the first.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
CHAPTER 9 SUBPROGRAMS Subprograms are useful in the following cases: long complicated program  Subprograms make it readable and simple by dividing it.
Module and Data Sharing. Programming in the Large Software, in general, is large having multiple units Multiple units designed and developed independently.
Week 3 Let's review! Fundamental data types List-directed input/output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Procedures and Modular Programming Part # 2. Interface Block ► Functions do not have to be internal to main program ► They can be stand-alone ► In this.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
More on F90 Outline: I.Parameter statements II.Comments III.Program layout-- execution part IV.if/then/else V.Case statements VI.do loops VII.goto VIII.Intrinsic.
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
Introduction to Fortran95 Programming Part II By Deniz Savas, CiCS, 2007
COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Web Database Programming Using PHP
Basic concepts of C++ Presented by Prof. Satyajit De
The Machine Model Memory
VBA - Excel VBA is Visual Basic for Applications
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2 - Introduction to C Programming
Web Database Programming Using PHP
September 8th.
Programming For Nuclear Engineers Lecture 6 Arrays
Organization of Programming Languages
Variables, Expressions, and IO
Prepared By: G.UshaRani B.Pranalini A.S.Lalitha
Array processing and Matrix manipulation
JavaScript: Functions.
Chapter 2 - Introduction to C Programming
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
User-Defined Functions
11/10/2018.
Subroutine Comp 208 Yi Lin.
One-Dimensional Array Introduction Lesson xx
7 Arrays.
By Deniz Savas, CiCS, Shef. Univ., 2015
Web DB Programming: PHP
Functions Jay Summet.
Java Programming Language
Data Structures & Algorithms
Chapter 9: Pointers and String
Programming Languages and Paradigms
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
Array processing and Matrix manipulation
Functions Jay Summet.
Presentation transcript:

mdul-cntns-sub-cmmnts2.f90 Interactive Commenting

Part 1 Module sub ! illustrates some non-executables and a subroutine in global module implicit none save ! ensure that values assigned to variables in module sub are retained integer, parameter :: n=5 ! non-executable: assign a value to a variable in a module integer :: i real, dimension (n) :: f,x,g contains ! ‘contains’ is method to include a subroutine Subroutine sign_chg(a) ! note use of underscore in a name is allowed real :: a(:) ! assumed shape array, it be one dimensional size specified by use. a = -a ! can use assign to change value of itself, even in this array arithmetic return ! optional ; note: can have multiple places to return from within a sub. end subroutine sign_chg end module sub ! end of the module, everything above is in this module Program testy2

Part 2 Program testy2 use sub ! insert info. in module sub; comes first, before implicit none in main prog. implicit none real :: pi, z(2*n) ! Alt. use of parameter in module; defines array z, twice size of n ! end of non-executables, executables follow (comment, not executed) pi = acos(-1.) ! a simple way to define pi Z = 1. ! fill array elements of z with the value 1. Call sign_chg(z) Print *, (z(2*i-1), i=1,n) ! print odd values of z x = (/(i*0.5, i=1,n)/) ! assign values to x with implied do loop in a data statement: / … / do i=1,n f(i) = x(i)**2 ! Use integer (not real) if power is int.; faster & avoids a type of error end do g=x**2 ! array arithmetic, calculates whole array g same result as for array f call printy(pi) ! illustrates calling a user-supplied subroutine call sign_chg(f) ! calls subroutine in the module that flips sign of f call printy(3.) ! passes pointer to dummy location where value 3. has been placed end program testy2

Part 3 end program testy2 subroutine printy(r) ! illustrates example of user-supplied subroutine use sub ! insert the information in the module (comes first, before implicit none) real :: r ! local variable definition (note: it is passed in subroutine argument list) print *, 'Printy input = ',r print *,' i x f g' ! simple spacing for column labels do i=1,n ! note that i and n are global variables passed here via module sub print 2,i,x(i),f(i),g(i) ! arrays x, f, and g passed via module sub end do print *, 'again using implied do loop' print 1 1 format(2x,'i',5x,'x',10x,'f',10x,'g') ! more clever spacing for column labels print 2,(i,x(i),f(i),g(i), i=1,n) ! another use of implied do loop in an I/O statement 2 format(i3,3f10.5) end subroutine printy

Output ./a.out -1.00000000 -1.00000000 -1.00000000 -1.00000000 -1.00000000 Printy input = 3.14159274 i x f g 1 0.50000 0.25000 0.25000 2 1.00000 1.00000 1.00000 3 1.50000 2.25000 2.25000 4 2.00000 4.00000 4.00000 5 2.50000 6.25000 6.25000 again using implied do loop i x f g Printy input = 3.00000000 1 0.50000 -0.25000 0.25000 2 1.00000 -1.00000 1.00000 3 1.50000 -2.25000 2.25000 4 2.00000 -4.00000 4.00000 5 2.50000 -6.25000 6.25000

Part 1 Module sub ! illustrates some non-executables and a subroutine in global module implicit none save ! ensure that values assigned to variables in module sub are retained integer, parameter :: n=5 ! non-executable: assign a value to a variable in a module integer :: i real, dimension (n) :: f,x,g contains ! ‘contains’ is method to include a subroutine Subroutine sign_chg(a) ! note use of underscore in a name is allowed real :: a(:) ! assumed shape array, it be one dimensional size specified by use. a = -a ! can use assign to change value of itself, even in this array arithmetic return ! optional ; note: can have multiple places to return from within a sub. end subroutine sign_chg end module sub ! end of the module, everything above is in this module Program testy2

Part 2 Program testy2 use sub ! insert info. in module sub; comes first, before implicit none in main prog. implicit none real :: pi, z(2*n) ! Alt. use of parameter in module; defines array z, twice size of n ! end of non-executables, executables follow (comment, not executed) pi = acos(-1.) ! a simple way to define pi Z = 1. ! fill array elements of z with the value 1. Call sign_chg(z) Print *, (z(2*i-1), i=1,n) ! print odd values of z x = (/(i*0.5, i=1,n)/) ! assign values to x with implied do loop in a data statement: / … / do i=1,n f(i) = x(i)**2 ! Use integer (not real) if power is int.; faster & avoids a type of error end do g=x**2 ! array arithmetic, calculates whole array g same result as for array f call printy(pi) ! illustrates calling a user-supplied subroutine call sign_chg(f) ! calls subroutine in the module that flips sign of f call printy(3.) ! passes pointer to dummy location where value 3. has been placed end program testy2

Part 3 end program testy2 subroutine printy(r) ! illustrates example of user-supplied subroutine use sub ! insert the information in the module (comes first, before implicit none) real :: r ! local variable definition (note: it is passed in subroutine argument list) print *, 'Printy input = ',r print *,' i x f g' ! simple spacing for column labels do i=1,n ! note that i and n are global variables passed here via module sub print 2,i,x(i),f(i),g(i) ! arrays x, f, and g passed via module sub end do print *, 'again using implied do loop' print 1 1 format(2x,'i',5x,'x',10x,'f',10x,'g') ! more clever spacing for column labels print 2,(i,x(i),f(i),g(i), i=1,n) ! another use of implied do loop in an I/O statement 2 format(i3,3f10.5) end subroutine printy