Subroutines and Functions

Similar documents
Subroutines, Functions and Modules

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

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

Page 1 of 7. Date: 1998/05/31 To: WG5 From: J3/interop Subject: Interoperability syntax (Part 1) References: J3/98-132r1, J3/98-139

SUBPROGRAMS AND MODULES

Introduction to Fortran Programming. -External subprograms-

Reusing this material

Review Functions Subroutines Flow Control Summary

Review More Arrays Modules Final Review

Goals for This Lecture:

NO CALCULATOR ALLOWED!!

Old Questions Name: a. if b. open c. output d. write e. do f. exit

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

Fortran 95/2003 Course

Introduction to Modern Fortran

1. User-Defined Functions & Subroutines Part 1 Outline

Subprograms. Bilkent University. CS315 Programming Languages Pinar Duygulu

Walt Brainerd s Fortran 90 programming tips

Modern Fortran OO Features

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

AMath 483/583 Lecture 8

PACKAGE SPECIFICATION HSL 2013

ISO/IEC : TECHNICAL CORRIGENDUM 2

Goals for This Lecture:

Chapter 4. Fortran Arrays

SUBROUTINE subroutine-name (arg1, arg2,..., argn)

Programming Languages

Fortran Coding Standards and Style

dbx90: Fortran debugger March 9, 2009

Concepts Introduced in Chapter 7

Goals for This Lecture:

Information technology Programming languages Fortran Part 1: Base language

Computers in Engineering. Subroutines Michael A. Hawker

Fortran 90 - A thumbnail sketch

SPECIFICATION 2 STATEMENTS

Computational Techniques I

Storage and Sequence Association

INF 212 ANALYSIS OF PROG. LANGS PROCEDURES & FUNCTIONS. Instructors: Kaj Dreef Copyright Instructors.

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

Extrinsic Procedures. Section 6

7. Procedures and Structured Programming

Chapter 6 Introduction to Defining Classes

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! CoCo Processor - Copyright (c) 1996 Imagine1, Inc.!!!!!!!!

Computational Astrophysics AS 3013

Our Strategy for Learning Fortran 90

Computer Programming: C++

International Standards Organisation. Parameterized Derived Types. Fortran

CHAPTER 4 FUNCTIONS. 4.1 Introduction

Lecture V: Introduction to parallel programming with Fortran coarrays

Evolution of Fortran. Presented by: Tauqeer Ahmad. Seminar on Languages for Scientific Computing

Procedural programs are ones in which instructions are executed in the order defined by the programmer.

Paul F. Dubois. X-Division.

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

Fortran classes and data visibility

Verification of Fortran Codes

TS Further Interoperability of Fortran with C WG5/N1917

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

C Programming for Engineers Functions

Chapter 5. Names, Bindings, and Scopes

ParaFEM Coding Standard for Fortran 90. Contents. 1.0 Introduction. 2.0 Documentation. 2.1 External Documentation

Fundamentals of Programming Session 13

LECTURE 14. Names, Scopes, and Bindings: Scopes

C interfaces to HSL routines. J. D. Hogg. Version 1.0 5th December Numerical Analysis Group Internal Report

Python, C, C++, and Fortran Relationship Status: It s Not That Complicated. Philip Semanchuk

Chapter 3. Fortran Statements

Advanced Features. SC10 Tutorial, November 15 th Parallel Programming with Coarray Fortran

Table 2 1. F90/95 Data Types and Pointer Attributes. Data Option. (Default Precision) Selected-Int-Kind

Fundamentals of Programming Session 12

Fundamentals of Programming Session 25

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 3:: Names, Scopes, and Bindings (cont.)

Module 3.7: nag ell fun Elliptic Functions. Contents

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

1 Chapter Plan...1 Exercise - Simple Program...2

1. User-Defined Functions & Subroutines Part 2 Outline

6.1 Expression Evaluation. 1 of 21

Appendix D. Fortran quick reference

A Brief Introduction to Fortran of 15

Computers in Engineering COMP 208. Subprograms. Subroutines. Subroutines Michael A. Hawker

CIS Intro to Programming in C#

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

15 FUNCTIONS IN C 15.1 INTRODUCTION

Goals for This Lecture:

Fortran Introduction

Chapter 6. A Brief Introduction to Fortran David A. Padua

Module 28.3: nag mv rotation Rotations. Contents

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Single-pass Static Semantic Check for Efficient Translation in YAPL

multiple variables having the same value multiple variables having the same identifier multiple uses of the same variable

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

A Catalog and Classification of Fortran Refactorings

Programming Tips for Plugins

The PCAT Programming Language Reference Manual

CS-201 Introduction to Programming with Java

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

write (unit=*,fmt=*) i =, i! will print: i = 3

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Module 5.5: nag sym bnd lin sys Symmetric Banded Systems of Linear Equations. Contents

Chapter 3:: Names, Scopes, and Bindings

Introduction to Fortran Programming. - Module -

Transcription:

Subroutines and Functions

Procedures: Subroutines and Functions There are two types of procedures: SUBROUTINE: a parameterized named sequence of code which performs a specific task and can be invoked from other program units. invoked with the CALL statement FUNCTION: same as a subroutine but returns a result in the function name. invoked by placing the function name (and its associated arguments in an expression) use when just one return value is needed. Example: sort3.f90 and sort_3.f90

Notes This simple example illustrates one of the important uses of subroutines: To exhibit the overall structure of a program and put the details in another place. Internal subroutines and functions are designated by the contains statement. The implicit none in the host program applies to the internal subroutines. Also used in modules. Can we go even further with this example? Look at sort_3a.f90

Subroutines with Arguments We can pass values to a subroutine by placing them in parentheses after the name of the subroutine in the call statement. In the call to swap, n1 and n2 are called arguments. To make subroutine swap available to other program units, we would need to place it within a module.

Functions Just like a subroutine, but intended to return one value (or an array of values). Invoked just like an intrinsic function. The result of a function should be placed in a result variable using the result keyword at the end of the function statement. If the result keyword and variable are omitted, the function name is used as the return variable and must be declared in the function) Example: series.f90

Argument Association The variables a and b in subroutine swap are place holders for the two numbers to be swapped. These are dummy arguments and must be declared in the subroutine. The variables n1 and n2 in the first call to swap are the actual arguments. If the value of a dummy argument changes, then so does the value of the actual argument (pass-byreference). An actual argument that is a constant or an expression more complicated than a variable can only pass a value to the corresponding dummy argument. This is called pass-by-value.

It is bad programming practice to modify arguments in function calls. In general, the number of actual and dummy arguments must be the same. Also, the data type (and kind parameter) of each actual argument must match that of the corresponding dummy argument. Keyword arguments and optional arguments: best explained by an example (series2.f90)

Argument Intent It s good programming practice to indicate whether a dummy argument will be: Only be referenced -- INTENT(IN) Be assigned to before use -- INTENT(OUT) Be referenced and assigned to -- INTENT(INOUT) The use of INTENT attributes is recommended as it: Allows good compilers to check for coding errors. Facilitates efficient compilation and optimization. Example: series3.f90

Scope The scope of a name is the set of lines in a Fortran program where that name may be used and refer to the same variable, procedure or type. In general, the scope extends throughout the program unit where the entity is declared (host association). Known to any procedures declared within. Example: calculatepay.f90 But NOT if the same entity is redeclared in an internal procedure. (myscope.f90) Module scope is a little different -- we ll cover that later (use association).

The Save Attribute Fortran 77 compilers generally used static storage for all variables. Most Fortran 90 systems use static storage only when required. This means that local variables in subroutines and functions will NOT be preserved after control returns unless: The variable is initialized. The SAVE attribute is used. real, save :: p, q There s also a SAVE statement but the use of the attribute in declarations is the preferred usage.

The Return Statement RETURN causes execution of a procedure to terminate with control given back to the calling program. Can be useful in more elaborate procedures as an alternative to a complicated set of nested if constructs.