Fortran- Subprograms Chapters 6, 7 in your Fortran book.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

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 4- Reading and Writing from Data Files Chapter 4 of your Fortran book.
Lecture 2 Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
1 ICS 101 – LAB 2 Arithmetic Operations I Putu Danu Raharja kfupm.edu.sa Information & Computer Science Department CCSE - King Fahd University.
Chapter 2 Basic Elements of Fortan
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
6 April, 2000 CS1001 Lecture 15 EXAM1 - results SUBPROGRAM - - revisit FUNCTION.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
 2006 Pearson Education, Inc. All rights reserved Arrays.
27 March, 2000 CS1001 Lecture 16 FUNCTIONS SUBROUTINES SCOPE MODULES EXTERNAL SUBPROGRAMS.
Chapter 9 Modules and Programming with Functions.
Chapter 6: User-Defined Functions I
Chapter 8 Arrays and Strings
Introduction to C Programming
6 April, 2000 CS1001 Lecture 13 PRACTICE EXAM - revisit SUBPROGRAM FUNCTION.
Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -
Chapter 15: Operator Overloading
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
FORTRAN PROGRAMMING BASICS MET 50. Programming Basics The basic layout of all programs is as follows (p.33) PROGRAM name = heading (i.e., title) Specifications.
VB .NET Programming Fundamentals
Basic Elements of C++ Chapter 2.
ReAl :: x OK CHARACTER :: name OK, a 1 character name! CHARACTER(LEN=10) :: name OK, string length 10 REAL :: var-1 cannot have -1 in a declaration var_1.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
 2006 Pearson Education, Inc. All rights reserved Arrays.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 8 Arrays and Strings
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
CHAPTER 9 SUBPROGRAMS Subprograms are useful in the following cases: long complicated program  Subprograms make it readable and simple by dividing it.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
CPS120: Introduction to Computer Science Functions.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
INPUT / OUTPUT STATEMENTS
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
FUNCTIONS IN FORTRAN For a complex program difficulties like writing it and debugging are encountered. These can be minimized by breaking main program.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 3: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
A First Book of ANSI C Fourth Edition
Chapter 02 (Part II) Introduction to C++ Programming.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
User-Written Functions
Functions and Subroutines
Chapter 2 - Introduction to C Programming
JavaScript: Functions.
Chapter 2 - Introduction to C Programming
Subroutine Comp 208 Yi Lin.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
JavaScript Arrays.
Chapter 2 - Introduction to C Programming
mdul-cntns-sub-cmmnts2.f90
Chapter 2 - Introduction to C Programming
Presentation transcript:

Fortran- Subprograms Chapters 6, 7 in your Fortran book

Statement Functions There may be a simple calculation that is used multiple times that is not an intrinsic function in Fortran If the computation can be written in a single assignment statement, we can use a statement function: Function name (argument list) = expression Statement functions are placed at the beginning of the program, preceding any executable statements. The function name and its arguments should be included in the type statement or it will be decided implicitly (integer or real based on first letter) ex: REAL CYVOL,HEIGHT,RAD CYVOL(RAD,HEIGHT)=3.14*RAD*HEIGHT

Notes on Functions Functions compute a single value Functions can never be used on the left side of an equal sign in an assignment statement The output of the function is determined intrinsically (I-N is an integer) or it must be specified in the type statement The arguments must be enclosed in parentheses The arguments may be constants, variables, expressions, or other functions

Subprograms Sometimes the same calculations must be used several times in a program or for different programs. Instead of writing the steps out each time, a subprogram can be added to the program that is referenced to by the main program. Subprograms make debugging programs simpler, increases readability of the program, and can be used multiple times and in different programs. Fortran has two types of subprograms: functions and subroutines

User-Defined Functions A function subprogram is like a program of its own that, instead of beginning with PROGRAM, begins with FUNCTION: FUNCTION name (argument list) and concludes with a RETURN statement preceding END that directs the program back to the main program The arguments in the main program must match the dummy arguments in the subprogram in type, number, and order If one of the arguments is an array, its dimensions must be specified in the main program and the subprogram Function subprograms return a single value that is stored in the function name A function can contain references to other functions, but not to itself The subprogram can be positioned before or after the main program or in a different file The same variable names can be used in the main program and subprogram without causing confusion The function name should be listed in the type statement in the main program and the subprogram

Example Here is an example of a program with a function subprogram: PROGRAM TEST INTEGER TEST1, TEST2, TEST3, AVE, TAVE READ*, TEST1, TEST2, TEST3 TAVE=AVE(TEST1,TEST2,TEST3) PRINT*, TAVE END ************************************* REAL FUNCTION AVE(A,B,C) REAL A,B,C AVE=(A+B+C)/3 RETURN END

Subroutines The subroutine is referenced from the main program with the following statement: CALL subroutine name (argument list) This statement will reference the subroutine that has the first line consisting of: SUBROUTINE subroutine name (argument list) There are a number of rules regarding writing and using subroutines: 1.The name of the subroutine has nothing to do with the type of data (real or integer) associated with the subroutine. 2.The first line of the subroutine identifies it as a subroutine, assigns a name that will be used to reference the subroutine, and also contains a list of arguments. 3.The list of arguments is necessary to transfer data and variables (input) to the subroutine as well as returning values to the calling program. The arguments that are used in the CALL statement are the actual arguments while the arguments in the SUBROUTINE statement are the dummy arguments. The arguments in the CALL statement must match in type, number and order those used in the subroutine definition. The actual variable names do not have to match, however if the first variable in the CALL statement is a real array, the first dummy argument in the SUBROUTINE statement must also be a real array. 4.A subroutine may return one or more values, or no values. 5.Variables used within a subroutine are therefore independent of variables used in the main program or in other subroutines. Any variables that are used in the subroutine that are not arguments are local variables, and the values are not accessible from the main program. Therefore if we have a variable named LENGTH in the main program, and the value of LENGTH is not transferred in the list of arguments, we can also have a variable LENGTH in the subroutine that is independent of the variable in the main program.

Subroutine Rules, continued 6.Be very careful with the dimension statements for multidimensional arrays in subroutines. You should always pass the dimensional size [DIMENSION A(100,100)], and the size which actually used [NROWS, NCOLS] as arguments. For example: Say that we have an array A in the main program that has been dimensioned: DIMENSION A(10,10) But we only have NCOLS=2, and NROWS=3 which are being used. If we have a subroutine SORT which we are transferring the array A in: CALL SORT (A,NCOLS,NROWS) In the subroutine we have (note the dummy arguments in the subroutine use NC for NCOLS and NR for NROWS) SUBROUTINE SORT(A,NC,NR) DIMENSION A(NR,NC). RETURN END Values in an array are actually stored in columns. When we dimension an array 10 x 10 we are reserving 100 locations in memory for A. The dimension statement in the SUBROUTINE SORT is only obtaining the first 6 (NC x NR) values from memory. Since the array is only stored in columns, we only be getting the first 6 values from column 1. To avoid this you should always provide the same array dimensioning in the main program as well as in subroutines.

Subroutine Rules, continued 7.Subroutines require a RETURN statement to return control to the main program or another subroutine that calls it. An END statement is also required. 8.A subroutine may reference other subroutines, however it cannot reference itself.