Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:

Slides:



Advertisements
Similar presentations
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.
Advertisements

Programming Languages and Paradigms
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
Chapter 6: User-Defined Functions I
Functions & Subroutines HTML version DFMain.chm. Types of subprograms Internal External Module Pure Elemental Recursive Optional arguments Generic Defined.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Fortran 9x HTML version. New F90 features Free source form Modules User-defined data types and operators Generic user-defined procedures Interface blocks.
6 April, 2000 CS1001 Lecture 15 EXAM1 - results SUBPROGRAM - - revisit FUNCTION.
27 March, 2000 CS1001 Lecture 16 FUNCTIONS SUBROUTINES SCOPE MODULES EXTERNAL SUBPROGRAMS.
Chapter 10 Modules and programming with subroutines.
Chapter 9 Modules and Programming with Functions.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 6: User-Defined Functions I
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 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
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.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Fortran: Specification Statements Session Six ICoCSIS.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Block-Structured Procedural Languages Lecture 11: Dolores Zage.
Introduction to FORTRAN
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Fortran: Program Units and Procedures Session Four ICoCSIS.
1 VBA – podstawowe reguły języka Opracowanie Janusz Górczyński wg Microsoft Help.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
JavaScript III Functions and Abstraction. 2 JavaScript so far statements assignment function calls data types numeric string boolean expressions variables.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
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.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Week 4: Organizational matters Register yourself to CIMS (Course Information and Management System) by clicking on Homework at hidiv.cc.itu.edu.tr/~F90.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Subroutines. Subroutine Subprograms Common features of Subroutines and functions –They perform particular tasks under the control of some other 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.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Functions and an Introduction to Recursion
Procedures and Modular Programming
Character (String) Data
-by Nisarg Vasavada (Compiled*)
User-Defined Functions
User Defined Functions
User Defined Functions
Presentation transcript:

Functions

Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms: –Internal, –External

Internal subprograms Internal Subprograms –are those routines that may appear within the main program by making use of the CONTAINS statement.

Function Subprograms The Fortran language provides many intrinsic, or library functions –ABS(X), SIN(X), REAL(X) Fortran allows the definition of additional functions –Programmer-defined Functions, or –Function Subprograms

PROGRAM Temperature_Conversion_1 REAL :: FahrenheitTemp, CelsiusTemp WRITE (*, '(1X, A)') "Enter a Fahrenheit temperature: " READ *, FahrenheitTemp CelsiusTemp = Fahr_to_Celsius(FahrenheitTemp) PRINT '(1X, 2(F6.2, A))', FahrenheitTemp, & " in Fahrenheit is = ", CelsiusTemp, " in Celsius" CONTAINS REAL FUNCTION Fahr_to_Celsius(Temp) REAL, INTENT(IN) :: Temp Fahr_to_Celsius = (Temp ) / 1.8 END FUNCTION Fahr_to_Celsius END PROGRAM Temperature_Conversion_1

Function Subprogram function heading specification part execution part END FUNCTION statement

Function Heading The function heading is a FUNCTION statement of the form: FUNCTION function-name (argument list) –where function-name may be any legal Fortran identifier –where argument list is an identifier or a list of identifiers separated by commas

Function Type Determining the type of FUNCTION –Type-identifier FUNCTION function (argument list) where type-identifier is the type of value returned by the function

Specification Part Same form as the specification part of a Fortran program with the additional stipulation that it must declare –The type of the function value –The type of each formal argument. These declarations should contain an INTENT specifier that tells how the arguments are to transfer the information.

Execution Part Same form as the execution part of a Fortran program with the additional stipulation that it should include at least one statement that assigns a value to the identifier that names the function. Function-name = expression

END FUNCTION The last statement of a function subprogram must be END FUNTION function-name

PROGRAM Temperature_Conversion_1 REAL :: FahrenheitTemp, CelsiusTemp WRITE (*, '(1X, A)') "Enter a Fahrenheit temperature: " READ *, FahrenheitTemp CelsiusTemp = Fahr_to_Celsius(FahrenheitTemp) PRINT '(1X, 2(F6.2, A))', FahrenheitTemp, & " in Fahrenheit is = ", CelsiusTemp, " in Celsius" CONTAINS REAL FUNCTION Fahr_to_Celsius(Temp) REAL, INTENT(IN) :: Temp Fahr_to_Celsius = (Temp ) / 1.8 END FUNCTION Fahr_to_Celsius END PROGRAM Temperature_Conversion_1

PROGRAM Temperature_Conversion_1 REAL :: FahrenheitTemp, CelsiusTemp WRITE (*, '(1X, A)') "Enter a Fahrenheit temperature: " READ *, FahrenheitTemp CelsiusTemp = Fahr_to_Celsius(FahrenheitTemp) PRINT '(1X, 2(F6.2, A))', FahrenheitTemp, & " in Fahrenheit is = ", CelsiusTemp, " in Celsius" CONTAINS FUNCTION Fahr_to_Celsius(Temp) REAL:: Fahr_to_Celsius REAL, INTENT(IN) :: Temp Fahr_to_Celsius = (Temp ) / 1.8 END FUNCTION Fahr_to_Celsius END PROGRAM Temperature_Conversion_1

SCOPE The scope of an entity (variables, constants, and subprograms) is the range within a program over which that entity has meaning or is visible. Where an entity is accessible and can be used.

Scope Principle The scope of an entity is the program or subprogram in which it is declared.

Scope Rule 1 An item declared within a subprogram is not accessible outside that subprogram –Such items are called local to that subprogram

Scope Rule 2 A global entity is accessible throughout the main program and in any internal subprogram in which no local entity has the same name as the global item.

SAVE Attribute The values of local variables in a subprogram are not retained from one execution of the subprogram to the next unless: –They are initialized in their declarations, –They are declared to have the SAVE attribute type, SAVE :: list-of-local-variables

External subprograms –Appear in a separate program section after the main programEND statement.

PROGRAM Temperature_Conversion_3 REAL :: Fahr_to_Celsius REAL :: FahrenheitTemp, CelsiusTemp CHARACTER(1) :: Response WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter a Fahrenheit temp: " READ *, FahrenheitTemp CelsiusTemp = Fahr_to_Celsius(FahrenheitTemp) PRINT '(1X, 2(F6.2, A))', FahrenheitTemp, & " in Fahrenheit is = to ", CelsiusTemp, " in Celsius" END PROGRAM Temperature_Conversion_3 FUNCTION Fahr_to_Celsius(Temp) IMPLICIT NONE REAL:: Fahr_to_Celsius REAL, INTENT(IN) :: Temp Fahr_to_Celsius = (Temp ) / 1.8 END FUNCTION Fahr_to_Celsius

Modules A module is a program unit used to package together type declarations, subprograms, and definitions of new data types. –MODULE name CONTAINS subprogram1 subprogram2 … END MODULE name

MODULE Temperature CONTAINS FUNCTION Fahr_to_Celsius(Temp) REAL:: Fahr_to_Celsius REAL, INTENT(IN) :: Temp Fahr_to_Celsius = (Temp ) / 1.8 END FUNCTION Fahr_to_Celsius FUNCTION Celsius_to_Fahr(Temp) REAL:: Celsius_to_Fahr REAL, INTENT(IN) :: Temp Celsius_to_Fahr = 1.8 * Temp END FUNCTION Celsius_to_Fahr END MODULE Temperature

Using a Module Once a module has been written, its contents can be made available to any other program unit by placing in that program unit a USE statement of the form USE module-name USE module-name ONLY: list

PROGRAM Temperature_Conversion_2 USE Temperature REAL :: FahrenheitTemp, CelsiusTemp CHARACTER(1) :: Response DO WRITE (*, '(1X, A)', ADVANCE = "NO") "Fahrenheit temp: " READ *, FahrenheitTemp CelsiusTemp = Fahr_to_Celsius(FahrenheitTemp) PRINT '(1X, 2(F6.2, A))', FahrenheitTemp, & " in Fahrenheit is = to ", CelsiusTemp, " in Celsius" WRITE (*, '(/ 1X, A)', ADVANCE = "NO") & "More temperatures to convert (Y or N)? " READ *, Response IF (Response /= "Y") EXIT END DO END PROGRAM Temperature_Conversion_2

Compiling & Linking Compilation –The source code is translated to an equivalent machine language program, called object program. Linking –External references to a program are resolved. References to functions contained in a module are linked to their definitions in that module, creating an executable program.

Compiling & Linking Source name: pgm6.f95 Module name: module6.f95 Compiling & Linking –f95 pgm6.f95 module6.f95 -o test6

Compiling & Linking f95 -c pgm6.f –creates the object file pgm6.o f95 -c module6.f –create the object file module6.o f95 pgm6.o module6.o -o test6 –Creates the executable file test6

Interfaces Explicit Interface –Internal subprograms –The compiler can check the number and type of arguments passed to a subprogram. Implicit Interface –External subprograms –The compiler may not be able to check whether references to a function are correct.

Interface Blocks Interface blocks can be used to provide explicit interfaces. The form needed for external subprograms is: INTERFACE Interface-body END INTERFACE

PROGRAM Temperature_Conversion_4 INTERFACE FUNCTION Fahr_to_Celsius(Temp) REAL:: Fahr_to_Celsius REAL, INTENT(IN) :: Temp END FUNCTION Fahr_to_Celsius END INTERFACE REAL :: FahrenheitTemp, CelsiusTemp CHARACTER(1) :: Response WRITE (*, '(1X, A)', ADVANCE = "NO") "Enter a Fahrenheit temp: " READ *, FahrenheitTemp CelsiusTemp = Fahr_to_Celsius(FahrenheitTemp) PRINT '(1X, 2(F6.2, A))', FahrenheitTemp, & " in Fahrenheit is = to ", CelsiusTemp, " in Celsius" END PROGRAM Temperature_Conversion_4 FUNCTION Fahr_to_Celsius(Temp) IMPLICIT NONE REAL:: Fahr_to_Celsius REAL, INTENT(IN) :: Temp Fahr_to_Celsius = (Temp ) / 1.8 END FUNCTION Fahr_to_Celsius