Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
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.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Chapter 5 Basic I/O Concepts Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel:
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
VBA Modules, Functions, Variables, and Constants
Chapter 8 Introduction to Arrays Part II Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Chapter 10 Additional Features of Arrays Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Functions & Subroutines HTML version DFMain.chm. Types of subprograms Internal External Module Pure Elemental Recursive Optional arguments Generic Defined.
Chapter 3 Program Design and Branching Structures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
Chapter 7 Introduction to Arrays Part I Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Chapter 2 Basic Elements of Fortan
Chapter 2: Input, Processing, and Output
Chapter 11 Additional Intrinsic Data Types Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
6 April, 2000 CS1001 Lecture 15 EXAM1 - results SUBPROGRAM - - revisit FUNCTION.
CS 201 Functions Debzani Deb.
Chapter 10 Modules and programming with subroutines.
Chapter 9 Modules and Programming with Functions.
Guide To UNIX Using Linux Third Edition
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
Chapter 1 Program Design
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Chapter 6 AN INTRODUCTION TO FILES AND FILE PROCESSING Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering.
Introduction to PL/SQL Chapter 9. Objectives Explain the need for PL/SQL Explain the benefits of PL/SQL Identify the different types of PL/SQL blocks.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Fortran: Specification Statements Session Six ICoCSIS.
Fortran: Program Units and Procedures Session Four ICoCSIS.
INTRODUCTION TO PROGRAMMING STRUCTURE Chapter 4 1.
Simple Program Design Third Edition A Step-by-Step Approach
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
CS0004: Introduction to Programming Subprocedures and Modular Design.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
CPS120: Introduction to Computer Science Functions.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
FUNCTIONS IN FORTRAN For a complex program difficulties like writing it and debugging are encountered. These can be minimized by breaking main program.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
VHDL Discussion Subprograms IAY 0600 Digital Systems Design Alexander Sudnitson Tallinn University of Technology 1.
Chapter 6 Methods Chapter 6 - Methods.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the program development.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Chapter 9: Value-Returning Functions
More About Objects and Methods
Chapter 2: Input, Processing, and Output
Procedures and Modular Programming
Chapter 7 Top-Down Development
Organization of Programming Languages
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Introduction to Classes and Objects
Chapter 2: Input, Processing, and Output
Presentation transcript:

Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul - Turkey Tel: +90 ( ) Fax: +90 ( )

Introduction to Procedures In Chapter 3, we learned the importance of good program design. The basic technique that we employed is top-down design. In top-down design, the programmer starts with a statement of the problem to be solved and the required inputs and outputs. Next, he or she describes the algorithm to be implemented by the program in broad outline, and applies decomposition to break the algorithm down into logical subdivisions called subtasks Fortunately, Fortran has a special mechanism designed to make subtasks easy to develop and debug independently before building the final program. It is possible to code each subtask as a separate program unit called an external procedure, and each external procedure can be compiled, tested, and debugged independently of all of the other subtasks (procedures) in the program. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 2

Introduction to Procedures There are two kinds of external procedures in Fortran: subroutines and function subprograms (or just functions). Subroutines are procedures that are invoked by naming them in a separate CALL statement, and that can return multiple results through calling arguments. Function subprograms are procedures that are invoked by naming them in an expression, and whose result is a single value that is used in the evaluation of the expression. Both types of procedures will be described in this chapter. Well-designed procedures enormously reduce the effort required on a large programming project. Their benefits include: 1.Independent testing of subtasks. 2.Reusable code. 3.Isolation from unintended side effects. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 3

Introduction to Procedures Subroutines A subroutine is a Fortran procedure that is invoked by naming it in a CALL statement and that receives its input values and returns its results through an argument list. The general form of a subroutine is © 2010, Dr. ALİ CAN TAKİNACI Slide No: 4 Name of the subroutine Dummy arguments End of the subroutine

Introduction to Procedures Any executable program unit may call a subroutine, including another subroutine. However, a subroutine may not call itself unless it is declared to be recursive; recursion will be explained in Chapter 13.) To call a subroutine, the calling program uses a CALL statement. The form of a CALL statement is where the order and type of the actual arguments in the argument list must match the order and type of the dummy arguments declared in the subroutine. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 5

Introduction to Procedures © 2010, Dr. ALİ CAN TAKİNACI Slide No: 6

Introduction to Procedures The INTENT Attribute Dummy subroutine arguments can have an INTENT attribute associated with them. The INTENT attribute is associated with the type declaration statement that declares each dummy argument. The attribute can take one of three forms: © 2010, Dr. ALİ CAN TAKİNACI Slide No: 7

Introduction to Procedures The purpose of the INTENT attribute is to tell the compiler how the programmer intends each dummy argument to be used. Some arguments may be intended only to provide input data to the subroutine, and some may be intended only to return results from the subroutine. Finally, some may be intended both to provide data and return results. The appropriate INTENT attribute should always be declared for each argument. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 8

Introduction to Procedures Variable Passing in Fortran: The Pass-by- Reference Scheme Fortran programs communicate with their subroutines using a pass-by- reference scheme. When a subroutine call occurs, the main program passes a pointer to the location in memory of each argument in the actual argument list. The subroutine looks at the memory locations pointed to by the calling program to get the values of the dummy arguments it needs. © 2010, Dr. ALİ CAN TAKİNACI Slide No: 9

Introduction to Procedures Passing Arrays to Subroutines There are three possible approaches to specifying the length of a dummy array in a subroutine. 1. Explicit-shape dummy array 2. Assumed-shape dummy arrays 3. Assumed-size dummy array. Not reccomended © 2010, Dr. ALİ CAN TAKİNACI Slide No: 10

Introduction to Procedures Passing Character Variables to Subroutines When a character variable is used as a dummy subroutine argument, the length of the character variable is declared with an asterisk. Since no memory is actually allocated for dummy arguments, it is not necessary to know the length of the character argument when the subroutine is compiled. A typical dummy character argument is shown below: © 2010, Dr. ALİ CAN TAKİNACI Slide No: 11

Introduction to Procedures FORTRAN FUNCTIONS A Fortran function is a procedure whose result is a single number, logical value, character string, or array. The result of a function is a single value or single array that can be combined with variables and constants to form Fortran expressions. These expressions may appear on the right side of an assignment statement in the calling program. There are two different types of functions: intrinsic functions (sin(x), log(x)) and user defined functions (or function subprograms). © 2010, Dr. ALİ CAN TAKİNACI Slide No: 12

Introduction to Procedures © 2010, Dr. ALİ CAN TAKİNACI Slide No: 13