CHAPTER 9 SUBPROGRAMS Subprograms are useful in the following cases: long complicated program  Subprograms make it readable and simple by dividing it.

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.
1 C++ Syntax and Semantics The Development Process.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Lecture 2 Introduction to C Programming
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Need for Arrays Exercise Read the IDs and the grades for all ICS 101 students. Compute and print the average of the students. Print the grades and IDs.
1 Module 2: Fundamental Concepts Problems Programs –Programming languages.
CHAPTER 8 ARRAYS  An array is a group of storage locations (elements) that have the same name.  The element of an array is distinguished by using the.
Intrinsic (build-in) Functions Examples of intrinsic functions: SQRT: y=sqrt(2.0) ABS: y=abs(x) SIN: y=sin(x) (x: radians) ATAN: pi=4.0*ATAN(1.0) When.
Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
SUB-PROGRAMS MET 50. Subroutines Programmers prefer to break the overall task into smaller pieces. Then we write individual “sub-programs” to do each.
Chapter 2 Basic Elements of Fortan
SUB-PROGRAMS MET 50. Using sub-programs The codes we/you have written so far are teeny ( lines). In science & engineering, many codes are huge.
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.
Revision – A simple program How to start a program? How to end a program? How to declare variables? What are the mathematical operators? How to start a.
Chapter 9 Modules and Programming with Functions.
Lecture Notes 8/30/05 Program Design & Intro to Algorithms.
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.
LAB-10 1-D Array I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
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.
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.
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.
COMP An Introduction to Computer Programming : University of the West Indies COMP6015 An Introduction to Computer Programming Lecture 02.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Introduction to FORTRAN
12-1 Structured COBOL Programming Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout John.
Fortran: Program Units and Procedures Session Four ICoCSIS.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
 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:
More While Loop Examples CS303E: Elements of Computers and Programming.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing.
User defined functions
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
FUNCTIONS IN FORTRAN For a complex program difficulties like writing it and debugging are encountered. These can be minimized by breaking main program.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
More on F90 Outline: I.Parameter statements II.Comments III.Program layout-- execution part IV.if/then/else V.Case statements VI.do loops VII.goto VIII.Intrinsic.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Programming Fundamentals Enumerations and Functions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Functions and Subroutines
Procedures and Modular Programming
Programming Problem steps must be able to be fully & unambiguously described Problem types; Can be clearly described Cannot be clearly described (e.g.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Subroutine Comp 208 Yi Lin.
Midterm Review Programming in Fortran
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
mdul-cntns-sub-cmmnts2.f90
REPETITION Why Repetition?
Presentation transcript:

CHAPTER 9 SUBPROGRAMS Subprograms are useful in the following cases: long complicated program  Subprograms make it readable and simple by dividing it into smaller programs. set of operations is to be repeated  Subprograms are used to avoid repetition.

FORTRAN language has two types of subprograms: FUNCTIONS: return single value. SUBROUTINES: return more than one value. Functions: There are two types of functions: INTRINSIC functions example: ConstantSIN (10.0) VariableSIN (theta) ExpressionSIN (2.0*alfa + theta/3.0) Other functionSIN (ABS (a-b))

USER-DEFINED functions Example: PROGRAM STATMENT_FUNCTION IMPLICIT NONE INTEGER,DIMENSION(3,4)::RENT INTEGER :: SET,I,J,L,K SET(K,L) = K +L/3 DO I=1,3 DO J=1,4 RENT(I,J)=SET(I,J) END DO PRINT*,'ELEMENTS OF ARRAY RENT ARE:' DO I=1,3 PRINT*,(RENT(I,J),J=1,4) END DO STOP END PROGRAM STATMENT_FUNCTION

External Function Subprogram External function subprogram can be made accessible to a program unit by attaching it after the END statement of the main program. EXAMPLE Write a main program to read three values and prints their average. The average values should be calculated using an external function subprogram. PROGRAM Test REAL :: Test 1, Test 2, Test 3, Ave READ *, Test 1, Test 2, Test 3 PRINT 5, Ave (Test 1, Test 2, Test 3) 5FORMAT (1X, ‘Average=’, F5.2) STOP END PROGRAM Test REAL FUNCTION Ave (X, Y, Z) REAL :: X, Y, Z Ave=(X+Y+Z)/3.0 RETURN END FUNCTION Ave

EXAMPLE : Follow the execution of the following program and show its output? PROGRAM FUNCTION_SUBPROGR AMS IMPLICIT NONE REAL :: A,B,C,TIME,ADD A = 1.0 B = 2.0 C = 3.0 TIME = 2.0*ADD(A,B,C) PRINT 10, TIME 10 FORMAT (1X, 'TIME=', F6.2) STOP END PROGRAM FUNCTION_SUBPROG RAMS ! ! FUNCTION SUBPROGRAM REAL FUNCTION ADD(X,Y,Z) REAL :: X,Y,Z Y = X +Y Z = Y + Z X = Z + X ADD = X + Y + Z RETURN END FUNCTION AD

Subroutine Subprogram  Subroutines are modulus written to perform operations that cannot be performed by a function. For example, if several values need to be returned from a module, a sub-routine is used.  A subroutine is also used for operations that do not compute values, such as reading the values in a data file.  Subroutines may be either internal or external. External Subroutines Subprogram External subroutines can be made accessible to a program unit by attaching it after the END statement of the main program.

Subroutines Rules 1.A subroutine does not represent a value; thus, its name should be chosen for documentation purposes and not to specify a real or integer value. 2.A subroutine is referenced with an executable statement whose general form is CALL subroutine name (argument list) 3.The first line in a subroutine identifies it and includes the subroutine name and argument list, as follows: SUBROUTINE name (argument list) 4.A subroutine uses the argument list for subroutine inputs and values returned to the calling program. arguments in CALL statement  actual arguments. arguments in SUBROUTINE  dummy arguments. The arguments in the CALL statement must match in type, number, and order those used in the subroutine definition.

5.A subroutine may return one value, many values, or no value. Similarly, a subroutine may have one input value, many input values, or no input value. 6.Arguments are the link between the main program and the subroutine. Thus, the choice of subroutine statement numbers and variable names is independent of those in the main program. Variables used in subroutine and not subroutine arguments  local variables  their values are inaccessible from main program. 7. The subroutine, like the function, requires a RETURN statement to return control to main program.It also requires an END statement. 8.A subroutine may reference other functions or call other subroutines, but it cannot call itself.

EXAMPLE: Write a Fortran program which uses a subroutine program to calculate the average, the minimum value and the maximum value of an array X with number of elements N represents the scores of students grade. PROGRAM Scores INTEGER :: N, I REAL :: Tests (100), Ave,Min,Max READ*, N READ*, (Tests(I), I=I,N) CALL Stat (Tests, N, Ave, Min, Max) Print 5, Ave, Min, Max 5Format (1X, 3F10.3) END PROGRAM Scores SUBROUTINE Stat (X, M, Xave, Xmin, Xmax) INTEGER :: M, I REAL :: X(M), Xave, Xmin, Xmax, Sum Sum=X(1) Xmax=X(1) Xmin=X(1) DO I=2,M Sum=Sum+X(I) IF (X(I) < Xmin) Xmin=X(I) IF (X(I) > Xmax) Xmax=X(I) END DO Xave=Sum/M RETURN END SUBROUTINE Stat

EXAMPLE Follow the execution of the following program and show its output? PROGRAM SUBROUTINE_EXAMPLE IMPLICIT NONE INTEGER,DIMENSION (10):: K INTEGER::I DO I=1,10 K(I)=I END DO CALL MODIFY (K,10) PRINT*,"NEW VALUES OF K ARE:" PRINT 15, (K(I),I=1,10) 15 FORMAT(1X,5I5) END PROGRAM SUBROUTINE_EXAMPLE SUBROUTINE MODIFY(L,N) INTEGER :: I,N INTEGER, DIMENSION (N):: L DO I=1,N L(I)=MOD(L(I),4) END DO RETURN END SUBROUTINE MODIFY