Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Modular Programming With Functions
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.
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
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
An Introduction to Programming with C++ Fifth Edition
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.
VBA Modules, Functions, Variables, and Constants
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
6 April, 2000 CS1001 Lecture 15 EXAM1 - results SUBPROGRAM - - revisit FUNCTION.
27 March, 2000 CS1001 Lecture 16 FUNCTIONS SUBROUTINES SCOPE MODULES EXTERNAL SUBPROGRAMS.
CS 201 Functions Debzani Deb.
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 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.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
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.
Large problems can be divided into smaller sub - problems ♦ each sub - problem can be solved separately in order to reach to the solution of the original.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Fortran: Specification Statements Session Six ICoCSIS.
Introduction to FORTRAN
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Fortran: Program Units and Procedures Session Four ICoCSIS.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:
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.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
CPS120: Introduction to Computer Science Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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.
Functions & Libraries. Introduction Functions – Modules of code Uses – Abstraction Build complex tasks with simpler ones Don't worry about details at.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
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.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 3: User-Defined Functions I
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 8 Scope, Lifetime, and More on Functions.
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.
Designing Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Functions and an Introduction to Recursion
Procedures and Modular Programming
CSC113: Computer Programming (Theory = 03, Lab = 01)
User-Defined Functions
C++ for Engineers and Scientists Second Edition
A First Book of ANSI C Fourth Edition
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Presentation transcript:

Chapter 6 Programming with Functions

FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions

Function Subprograms The subprogram can be made accessible to a program, called the main program, in three ways: Internal subprogram Module subprogram External subprogram

Argument Association (Figure 6.1)

Fundamental 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. 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.

Saving the Values of Local Variables The values of local variables in subprogram are not retained from one execution of subprogram to the next unless either: 1. they are initialized in their declarations, or 2. The are declared to have the SAVE attribute.

Saving the Values of Local Variables FUNCTION F (......) INTEGER :: Count = Count = Count END FUNCTION F (......) or by using SAVE statement

Declaring a Function ’ s Type FUNCTION Atomic_Number (X, Y) INTEGER :: Atomic_Number REAL, INTENT (IN) :: X, Y ……….. ……….. ……….. END FUNCTION Atomic_Number INTEGER FUNCTION Atomic_Number (X, Y) REAL, INTENT (IN) :: X, Y ……….. ……….. ……….. END FUNCTION Atomic_Number

Introduction to Modules ( 模組 ) MODULE module-name CONTAINS subprogram_1 subprogram_2 . subprogram_n END MODULE module-name Purpose: Packages subprogram_1, subprogram_2, ..., subprogram_n together into a library that can be used in any other program unit.

Using Module: USE Statement Forms: USE module-name USE module-name, ONLY: list USE module-name, rename-list Examples: Figure 6.8 and Figure 6.9 USE Temperature_Library, ONLY: Fahr_to_Celsius

Compiling and Linking Programs and Modules

Compilation, in which the source program is translated to an equivalent machine-language, called an object program, which is stored in an object file. Linking, in which any references to functions contain a module are linked to their definitions in that module, creating an executable program, which is stored in an executable file.

Compiling and Linking Programs and Modules Separate compilation of program ’ s source file, creating an object file. Separate compilation of the module, creating a different object file. Linking the function calls the program ’ s object file to function definitions in the module ’ s object file, creating an executable program.

External Functions EXTERNAL ( 外界的 ) SUBPROGRAMS Example: Figure 6.10 INTERFACES ( 界面 ) Internal subprogram: explicit ( 明確的 ) interface External subprogram: implicit ( 不明確的;含蓄 的 ) interface To ensure that a compiler can perform the necessary consistency check, it is desirable that external subprograms have explicit interfaces.

Interface Blocks Interface blocks can be used to provide these explicit interfaces. INTERFACE Interface-body END INTERFACE Example: Figure 6.11

Chapter 7 Programming with Subroutines

Programming with Subprograms Subprograms in Fortran can be either functions or subroutines. Complex problems are best solved by dividing them into simpler sub-problems and designing subprograms to solve these sub-problems.

Subroutine Subprogram subroutine heading specification part execution part END SUBROUTINE statement Subroutine Heading : SUBROUTINE subroutine-name (formal-argument-list) CALL Statement: CALL SUBROUTINE subroutine-name (actual-argument-list)

Example: Displaying an Angle in Degrees An angular measurement in degrees, minutes, and seconds, say 100° 30' 36 ″ is equivalent to ° CALL PrintDegrees(NumDegrees, & NumMinutes, NumSeconds)

Figure 7.1: Displaying an Angle in Degrees SUBROUTINE PrintDegrees (Degrees, Minutes, Seconds) INTEGER, INTENT(IN) :: Degrees, Minutes, Seconds PRINT 10, Degrees, Minutes, Seconds, & REAL(Degrees) + REAL(Minutes)/ & REAL(Seconds)/ FORMAT (1X, I3, " degrees", I3, " minutes", I3, & " seconds" / 1X, "is equivalent to" / 1X, F7.3, & " degrees") END SUBROUTINE PrintDegrees

Subroutine Subprograms Subroutine subprograms have many features in common with function subprograms: They are program units designed to perform particular tasks under the control of some other program unit. The have the same basic form: each consists of a heading, a specification part, an execution part, and an END statement. They may be internal, module, or external subprogram

Subroutine Subprograms The different, however, in the following respects: Functions are designed to return a single value to the program unit that references them. Subroutines often return more than one value, or they may return no value at all but simple perform some task such as displaying a list of instructions to the user. Functions return values via function names; subroutines return values via arguments. A function is referenced by using its name in an expression, where a subroutine is referenced by a CALL statement.

Example of a Subroutine That Returns Values: Converting Coordinates Figure 7.3 x = r × cos θ y = r × sin θ !-Convert_to_Rectangular ! Subroutine to convert polar coordinates (R, Theta) to rectangular ! coordinates (X, Y). ! ! Accepts: Polar coordinates R and Theta (in radians) ! Returns: Rectangular coordinates X and Y !

Converting Coordinates SUBROUTINE Convert_to_Rectangular(R, Theta, X, Y) REAL, INTENT(IN) :: R, Theta REAL, INTENT(OUT) :: X, Y X = R * COS(Theta) Y = R * SIN(Theta) END SUBROUTINE Convert_to_Rectangular

Argument Association CALL Convert_to_Rectangular & (RCoord, TCoord, & XCoord, YCoord) INTENT (IN) attribute INTENT (OUT) attribute INTENT (INOUT) attribute

7.3 Random numbers Generators Some initial value called seed is required to begin the process of generating random numbers Each random number produced is used in computation of the next random number. Fortran 90 provides the subroutine RANDOM_SEED to initial the random number generator RANDOM_NUMBER, which is a subroutine that produces random real numbers uniformly distributed over the range 0 to 1.

Example Dice Tossing (Figure 7.6) ! Seed the random number generator CALL RANDOM_SEED ....... CALL RANDOM_NUMBER(R1) CALL RANDOM_NUMBER(R2) Die_1 = 1 + INT(6*R1) Die_2 = 1 + INT(6*R2) Pair = Die_1 + Die_2

Normal Distributions

Probabilities for Dice Tossing

7.6 Subprograms as Arguments

Subprograms as Arguments In our examples of subprograms that far, the actual arguments have been constants, variables, or expressions, but Fortran also permits functions and subroutine as arguments for other subprograms. In this case, the function or subroutine must be a module subprogram, an external subprogram, or an intrinsic subprogram. Also, no INTENT attribute is used for formal argument that is a subprogram.

Module containing a function Integrand (Figures 7.11 & 7.12) MODULE Integrand_Function CONTAINS FUNCTION Integrand(X) REAL :: Integrand REAL, INTENT(IN) :: X Integrand = EXP(X**2) END FUNCTION Integrand END MODULE Integrand_Function

Module Subprograms as Arguments PROGRAM Definite_Integral_2 USE Integrand_Function IMPLICIT NONE REAL :: A, B INTEGER :: Number_of_Subintervals WRITE (*, '(1X, A)', ADVANCE = "NO") & "Enter the interval endpoints and the # of subintervals: " READ *, A, B, Number_of_Subintervals CALL Integrate(Integrand, A, B, Number_of_Subintervals)

External Subprograms as Arguments type, EXTERNAL :: function-name Figure 7.13, p. 480

Intrinsic Subprograms as Argument Integrand = SIN (x) Figure 7.14, p. 481

Interface Blocks INTERFACE FUNCTION Integrand(X) REAL :: Integrand REAL, INTENT(IN) :: X END FUNCTION Integrand END INTERFACE

Interface Blocks (Figure 7.15, p. 483) REAL FUNCTION Integrand(X) REAL, INTENT(IN) :: X Integrand = EXP(X**2) END FUNCTION Integrand

7.7 Optional and Keyword Arguments of Subprograms A + BX + CX 2 +DX 3 + EX 4 FUNCTION Polynomial (X, A, B, C, D, E) REAL :: Polynomial REAL, INTENT (IN) :: X, A, B, C, D, E Polynomial = A + B*X + C*X**2 & + D*X**3 + E*X**4 END FUNCTION Polynomial

Keyword Arguments (Figure 7.16) FUNCTION Polynomial(X, A, B, C, D, E) REAL :: Polynomial REAL, INTENT(IN) :: X, A REAL, INTENT(IN), OPTIONAL :: B, C, D, E Polynomial = A IF (PRESENT(B)) Polynomial = Polynomial + B*X IF (PRESENT(C)) Polynomial = Polynomial + C*X**2 IF (PRESENT(D)) Polynomial = Polynomial + D*X**3 IF (PRESENT(E)) Polynomial = Polynomial + E*X**4 END FUNCTION Polynomial