Subroutines. Subroutine Subprograms Common features of Subroutines and functions –They perform particular tasks under the control of some other program.

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.
ET-314 Week 11 Nov 7, 2014 Topic(s): 19.1 Vectors.
In this section, we will study the following topics:
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
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.
Polar Coordinates (MAT 170) Sections 6.3
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.
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.
6 April, 2000 CS1001 Lecture 13 PRACTICE EXAM - revisit SUBPROGRAM FUNCTION.
21 April, 2000 CS1001 Lecture 27 Final Examination Review.
1 CS 415: Programming Languages Fortran Aaron Bloomfield Fall 2005.
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.
Python Programming Fundamentals
Fortran: Specification Statements Session Six ICoCSIS.
Introduction to FORTRAN
Fortran: Program Units and Procedures Session Four ICoCSIS.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
POLAR COORDINATES (Ch )
6.3 Polar Coordinates Day One
10.4A Polar Equations Rectangular: P (x, y) Polar: P (r,  )  r = radius (distance from origin)   = angle (radians)
Chapter 13 Section 3 Radian Measure.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
Polar Coordinates Lesson Points on a Plane Rectangular coordinate system  Represent a point by two distances from the origin  Horizontal dist,
Some Advanced Features of Procedures. Recursion Recursive Calls –A procedure can call itself (Self Recursion) –A can call B, B calls C, etc, Z calls A.
Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:
DeMoivre’s Theorem The Complex Plane. Complex Number A complex number z = x + yi can be interpreted geometrically as the point (x, y) in the complex plane.
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.
Intro to Polar Coordinates Lesson 6.5A. 2 Points on a Plane  Rectangular coordinate system Represent a point by two distances from the origin Horizontal.
Polar Coordinates In this section, we will study the following topics: Converting between polar and rectangular coordinates.
Week 3 Let's review! Fundamental data types List-directed input/output.
Slide Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
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.
Section 6.5 Complex Numbers in Polar Form. Overview Recall that a complex number is written in the form a + bi, where a and b are real numbers and While.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
EGR 115 Introduction to Computing for Engineers User-Defined Functions1 – Part 1 Wednesday 22 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Polar Coordinates Lesson Points on a Plane Rectangular coordinate system  Represent a point by two distances from the origin  Horizontal dist,
Subroutines II. An Extended Example Subroutines are best used to simplify large and unwieldy programs. Here is one example of why and how they should.
Fortran MATTHEW CARSON. History of FORTRAN FORTRAN ; First High Level Language FORTRAN I 1957; First Compiled Language FORTRAN II ; Independent.
Applications of Trigonometric Functions
Week 7 : String and photo processing. Today’s Tasks  Practice list and string  Convert Decimal to any radix base number  Between Binary and Hexadecimal.
Designing Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Unit Circle ( √3, 1 ) 2 2 ( 1, √3 ) 2 2 ( √2, √2 ) ˚ 45˚ 60˚
14.1 The Unit Circle Part 2. When measuring in radians, we are finding a distance ____ the circle. This is called. What is the distance around a circle?
Polar Coordinates Polar and Rectangular Coordinates Packet 3.
Numeric Functions Purpose:-
Chapter 6 - Functions modular programming general function format
Character (String) Data
JavaScript: Functions
Trigonometric Function: The Unit circle
Midterm Review Programming in Fortran
Chapter 7: Using Functions, Subs, and Modules
Lecture 4 : January 2001 Dr. Andrew Paul Myers
Polar Coordinates Polar and Rectangular Coordinates
Convert the following to rectangular form:
13.3 Polar Coordinates Rita Korsunsky.
Graphing Polar Coordinates
Presentation transcript:

Subroutines

Subroutine Subprograms Common features of Subroutines and functions –They perform particular tasks under the control of some other program unit. –They have the same basic form: Heading Specification Part Execution Part END statement

Subroutine Subprograms Common features of Subroutines and functions –They may be internal, module, or external. –Same scope rules apply to both. –They may be used as arguments –They may be recursive

Subroutine Subprograms Subroutines and functions differ in...: –Functions are designed to return a single value, Subroutines often return more than one value (or they may return no value at all). –Functions return values via functions names; subroutines return values via arguments. –A function is referenced by using its name in an expression, whereas a subroutine is referenced by a CALL statement

The form of a subroutine SUBROUTINE name (formal-argument-list) Specification Part Execution Part END SUBROUTINE statement

Invoking a Subroutine CALL subroutine-name (actual-argument-list)

PROGRAM Angles_1 IMPLICIT NONE INTEGER :: NumDegrees, NumMinutes, NumSeconds CHARACTER(1) :: Response ! Read and convert angles until user signals no more data DO WRITE (*, '(1X, A)', ADVANCE = "NO") & "Enter degrees, minutes, and seconds: " READ *, NumDegrees, NumMinutes, NumSeconds CALL PrintDegrees(NumDegrees, NumMinutes, NumSeconds) WRITE (*, '(/ 1X, A)', ADVANCE = "NO") "More (Y/N)? " READ *, Response IF (Response /= "Y") EXIT END DO

CONTAINS 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 END PROGRAM Angles_1

Converting Coordinates Example X= r cos Ø Y= r sin Ø P(r, Ø) r Ø

PROGRAM Polar_to_Rectangular IMPLICIT NONE REAL :: RCoord, TCoord, XCoord, YCoord CHARACTER(1) :: Response DO WRITE (*, '(1X, A)', ADVANCE = "NO") & "Enter polar coordinates (in radians): " READ *, RCoord, TCoord CALL Convert_to_Rectangular(RCoord, TCoord, XCoord, YCoord) PRINT *, "Rectangular coordinates:", XCoord, YCoord WRITE (*, '(/ 1X, A)', ADVANCE = "NO") & "More points to convert (Y or N)? " READ *, Response IF (Response /= "Y") EXIT END DO

CONTAINS 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 END PROGRAM Polar_to_Rectangular