Reusing this material

Similar documents
Overloading, abstract classes, and inheritance

Welcome. Modern Fortran (F77 to F90 and beyond) Virtual tutorial starts at BST

Introduction to Object- Oriented Programming

Fortran classes and data visibility

Introduction to Fortran Programming. -Internal subprograms (1)-

Subroutines and Functions

FORTRAN 90: Functions, Modules, and Subroutines. Meteorology 227 Fall 2017

Review More Arrays Modules Final Review

Introduction to Fortran Programming. -External subprograms-

Subroutines, Functions and Modules

Chapter 5 Object-Oriented Programming

Introduction to Programming Using Java (98-388)

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

Message-Passing Programming with MPI. Message-Passing Concepts

Introduction to Fortran Programming. - Module -

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Software Development. Modular Design and Algorithm Analysis

Lecture 5: Methods CS2301

Object Oriented Programming. Solved MCQs - Part 2

C++ Important Questions with Answers

Message Passing Programming. Designing MPI Applications

Computational Techniques I

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

Goals for This Lecture:

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Advanced Fortran Programming


Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

QUIZ. How could we disable the automatic creation of copyconstructors

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

The return Statement

ENCAPSULATION. private, public, scope and visibility rules. packages and package level access.

QUIZ. How could we disable the automatic creation of copyconstructors

SUBPROGRAMS AND MODULES

Modern Fortran OO Features

Fortran Coding Standards and Style

Advanced OpenMP. Memory model, flush and atomics

Java 8 Programming for OO Experienced Developers

Review Functions Subroutines Flow Control Summary

Java Object Oriented Design. CSC207 Fall 2014

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview

S.No Question Blooms Level Course Outcome UNIT I. Programming Language Syntax and semantics

OUTCOMES BASED LEARNING MATRIX

Extrinsic Procedures. Section 6

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Non-Blocking Communications

Fortran 95/2003 Course

Message Passing Programming. Modes, Tags and Communicators

2 3. Syllabus Time Event 9:00{10:00 morning lecture 10:00{10:30 morning break 10:30{12:30 morning practical session 12:30{1:30 lunch break 1:30{2:00 a

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

1. Write two major differences between Object-oriented programming and procedural programming?

CGS 2405 Advanced Programming with C++ Course Justification

Welcome to Design Patterns! For syllabus, course specifics, assignments, etc., please see Canvas

XII CS(EM) Minimum Question List N.KANNAN M.Sc., B.Ed COMPUTER SCIENCE IMPORTANT QUESTION (TWO MARKS) CHAPTER 1 TO 5 ( STAR OFFICE WRITER)

Tokens, Expressions and Control Structures

Introduction to Object-Oriented Concepts in Fortran95

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

2.4 Structuring programs

What does it mean by information hiding? What are the advantages of it? {5 Marks}

Reusing this material

Object Oriented Programming with Java. Unit-1

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Goals for This Lecture:

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

Part 7 - Object Oriented Concepts Classes, Objects, Properties and Methods

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design

Goals for This Lecture:

Programming II (CS300)

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Example: Fibonacci Numbers

Informatica 3 Syntax and Semantics

Object Orientated Analysis and Design. Benjamin Kenwright

CSE 307: Principles of Programming Languages

CO Java SE 8: Fundamentals

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Classes, Objects, and OOP in Java. June 16, 2017

CS 162, Lecture 25: Exam II Review. 30 May 2018

Lecture Notes on Programming Languages

In context with optimizing Fortran 90 code it would be very helpful to have a selection of

Lecture 18 Tao Wang 1

Functions, Scope & Arguments. HORT Lecture 12 Instructor: Kranthi Varala


Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

CSC 533: Organization of Programming Languages. Spring 2005

Binding and Variables

7. Procedures and Structured Programming

Computational Astrophysics AS 3013

Classwide Programming 1

OBJECT ORİENTATİON ENCAPSULATİON

MIDTERM EXAMINATION - CS130 - Spring 2003

VALLIAMMAI ENGINEERING COLLEGE

Walt Brainerd s Fortran 90 programming tips

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Building Multitier Programs with Classes


Transcription:

Modules

Reusing this material This work is licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International License. http://creativecommons.org/licenses/by-ncsa/4.0/deed.en_us This means you are free to copy and redistribute the material and adapt and build on the material under the following terms: You must give appropriate credit, provide a link to the license and indicate if changes were made. If you adapt or build on the material you must distribute your work under the same license as the original. Note that this presentation may contain images owned by others. Please seek their permission before reusing these images.

Program units Could write complete program as a single unit Preferable to break the program into smaller more manageable units In Fortran there are three types of program unit Main program External subprogram (e.g. library routines) Module Program units Perform simple manageable task(s) Can be written, compiled and tested in isolation Built up to form the complete program

Modules Constants, variables, and procedures can be encapsulated in modules for use in one or more programs. A module is a collection of variables and procedures module sort implicit none! variable specifications... contains! procedure specifications subroutine sort_sub1()... end subroutine sort_sub1... end module sort Variables declared above contains are in scope Everywhere in the module itself Can also be made available by using the module

Points about modules Within a module, functions and subroutines are known as module procedures Module procedures can contain internal procedures Module objects can be given the SAVE attribute Modules can be USEd by procedures and modules Modules must be compiled before the program unit which uses them.

Module syntax MODULE module-name [ <declarations and specification statements> ] [ CONTAINS <module-procedures> ] END [ MODULE [ module-name ]]

Module example MODULE Triangle_Operations IMPLICIT NONE REAL, PARAMETER :: pi=3.14159 CONTAINS FUNCTION theta(x,y,z)... END FUNCTION theta FUNCTION Area(x,y,z)... END FUNCTION Area END MODULE Triangle_operations

Using modules Contents of a module are made available with use : PROGRAM TriangUser USE Triangle_Operations IMPLICIT NONE REAL :: a, b, c The use statement(s) should go directly after the program statement implicit none should go directly after any use statements There are important benefits Procedures contained within modules have explicit interfaces Number and type of the arguments is checked at compile time Not the case for external procedures Can implement data hiding or encapsulation Via public and private statements and attributes

Restricting visibility The visibility of an object declared in a module can be restricted to that module by giving it the attribute PRIVATE REAL :: Area, theta PUBLIC!confirm default PRIVATE :: theta!restrict REAL, PRIVATE :: height!restrict All variables are available within the module But can only use public objects The default case is public

USE rename syntax Can rename module variables and procedures when using them: USE <module-name> & [,<new-name> => <use-name>] i.e. USE Triangle_Operations, & Space => Area

USE ONLY syntax Also possible to restrict what parts of a module to use: USE <module-name> [, ONLY : <only-list>] i.e. USE Triangle_operations, ONLY: & pi, Space => Area

interface [name] [Interface specification part] end interface [name] Module interfaces Fortran allows the definition of interfaces Informs compiler of expected shape, type, and number of arguments for routine or function (also optional nature, intent) Can provide Compile time checking and aid to debugging code Potential increase in efficiency Can have explicit interfaces, i.e.: interface real function fun(x) real, intent(in) :: x end function fun end interface Not necessary to specify explicit interfaces for module procedures

Module interfaces Possible to implement polymorphism with module interfaces, i.e.: module maths_functions implicit none private public :: my_sum interface my_sum module procedure real_sum module procedure int_sum end interface contains function real_sum (a, b) implicit none real, intent(in) :: a,b real_sum = a + b end function real_sum function int_sum (a, b) implicit none integer, intent(in) :: a,b int_sum = a + b end function int_sum end module

Operator overloading Using interfaces it is possible to overload operators (or define your own operators) as well: implicit none private interface operator(+) module procedure real_sum, int_sum end interface contains Only really makes sense if you define your own operators or datatypes Can t override existing definitions (the above example isn t actually allowed)

Psuedo OO programming with F90 Modules and interfaces allow semi-oo programming Encapsulation of data and functions with modules Controlled access to data or functions with private and public keywords Polymorphism with interfaces Operator overloading with interfaces Does not provide full OO functionality but can be very powerful Often enough functionality with this without using the F2003 additions

Exercise Look at the basic module creation practicals Move on to covert percolate source code from single file to multiple modules

Compiling code with modules Consider the program main (main.f90) which uses module sort (sort.f90) program main use sort implicit none... call sort_sub1() end program main main.f90 and sort.f90 are separate files To compile this program use gfortran sort.f90 main.f90 o progsort As the program main uses module sort, sort should be compiled before main

Compiling code with modules If you execute the command gfortran sort.f90 main.f90 o progsort You will notice that a file with a.mod extension is created for each module file For this example a file sort.mod will be created These.mod files contain information about global files and interfaces

Some dos and don ts Can have: module a end module a module b use a end module b program c use b end program c But not: module a use b end module a module b use a end module b