SUB-PROGRAMS MET 50. Using sub-programs The codes we/you have written so far are teeny (10- 40 lines). In science & engineering, many codes are huge.

Slides:



Advertisements
Similar presentations
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Advertisements

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.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
17 March, 2000 CS1001 Lecture 2 Programming and problem solving Software engineering practices.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Software engineering, program management. The problem  Software is expensive to design! – Industry estimates put software development labor costs at.
SUB-PROGRAMS MET 50. Subroutines Programmers prefer to break the overall task into smaller pieces. Then we write individual “sub-programs” to do each.
MORE ON… MAKING YOUR OUTPUT LOOK NICE! MET 50. Pretty output Recall the more sophisticated PRINT statement: PRINT nn, GRAV nn FORMAT (stuff) We use the.
LAB-05 Function I Putu Danu Raharja Information & Computer Science Department CCSE - King Fahd University of Petroleum & Minerals.
6 April, 2000 CS1001 Lecture 15 EXAM1 - results SUBPROGRAM - - revisit FUNCTION.
CS 201 Functions Debzani Deb.
Chapter 10 Modules and programming with subroutines.
Introduction to Computers and Programming Lecture 13: User defined methods Instructor: Evan Korth New York University.
REPETITIVE EXECUTION MET 50. FABULOUS “DO LOOPS” MET 50.
Chapter 9 Modules and Programming with Functions.
Modules, Hierarchy Charts, and Documentation
Understanding the Mainline Logical Flow Through a Program (continued)
Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
FORTRAN PROGRAMMING BASICS MET 50. Programming Basics The basic layout of all programs is as follows (p.33) PROGRAM name = heading (i.e., title) Specifications.
Chapter 3 Planning Your Solution
Introduction to Methods
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.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Computer Science 101 Introduction to Programming.
Introduction to FORTRAN-90 University of Liverpool course.
1 -Defined Functions 1. Goals of this Chapter 2. General Concept 3. Advantages 4. How it works Programmer.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Computer Science 101 Introduction to Programming.
E0001 Computers in Engineering Procedures: subprograms and 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:
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
CHAPTER 9 SUBPROGRAMS Subprograms are useful in the following cases: long complicated program  Subprograms make it readable and simple by dividing it.
IXA 1234 : C++ PROGRAMMING CHAPTER 1. PROGRAMMING LANGUAGE Programming language is a computer program that can solve certain problem / task Keyword: Computer.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 6 Defining Functions.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
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.
Beginning Fortran Fortran (77) Advanced 29 October 2009 *Black text on white background provided for easy printing.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Chapter 1: Introduction to Computers and Programming.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
2.1 First C Program. First Program Open visual studio, click new file Save as “programName.c” – Program must start with letter and have no spaces – Must.
Subprograms Functions Procedures.
Chapter Topics The Basics of a C++ Program Data Types
COMPUTATIONAL CONSTRUCTS
Learning to Program D is for Digital.
Procedures and Modular Programming
Basic Elements of C++.
Introduction to Programming
CMSC201 Computer Science I for Majors Lecture 11 – Program Design
Basic Elements of C++ Chapter 2.
TRANSLATORS AND IDEs Key Revision Points.
Chapter 4 void Functions
First Python Program Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An.
LAB-06 IF + Functions I Putu Danu Raharja
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Topics Introduction to Functions Defining and Calling a Function
The structure of programming
Introducing Modularity
Presentation transcript:

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. Thousands of lines of code. 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 2

Using sub-programs Programmers % prefer to break the overall task into smaller pieces. Then we write individual “sub-programs” to do each task! Each piece is a module. 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 3

Using sub-programs Modular programming : GFDL site:  → model development  → atmospheric  → model development  → The AM2 model  → Fortran module documentation  → huge list of modules (e.g., ice-model.html) 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 4

Using sub-programs Example: To make a weather forecast…. We need to pay attention to: 1. Solar radiation heating – compute in one code 2. Longwave radiation cooling – compute in one code 3. Cloud cover – compute in one code 4. Evaporation from the ocean – compute in one code 5. etc etc. 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 5

Using sub-programs It makes sense to write a separate chunk of code for each task! Makes initial development easier Allows upgrades easily 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 6

Using sub-programs Types of sub-program: (A) Functions – simpler (B) Subroutines – more sophisticated 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 7

Using sub-programs In BOTH cases, we: 1. Send some information from the main program to the sub-program 2. The sub-program does some calculation(s) 3. The sub-program returns an value to the main program. 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 8

Using sub-programs (A) Functions: We have already met intrinsic functions. Built in to the Fortran compiler. Things like “sin(x)” When we write: Y = SIN(X), our code is calling an (intrinsic) function. 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 9

Using sub-programs We don’t get to see the code behind these. Sometimes, we build our own functions! The code looks like: 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 10

Using sub-programs FUNCTION name (arguments) Usual Fortran statements Execution statements RETURN END FUNCTION name name has to obey usual rules “arguments” are values sent over from the main program “RETURN” statement sends results back to the main program 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 11

Using sub-programs Example: FUNCTION f_to_c (Temperature) ! or… REAL FUNCTION f_to_c (Temperature) ! ! Input: Temperature = temp in deg F ! Output: f_to_c = temp in deg C ! IMPLICIT NONE REAL :: f_to_c, Temperature ! f_to_c = (Temperature )*9./5. ! RETURN END FUNCTION f_to_c 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 12

Using sub-programs How is this module “summoned” from the main code??? PROGRAM TEMPCON ! This code reads in a temperature (F) and converts to Celcius IMPLICIT NONE REAL :: TEMPIN, TEMP ! PRINT*,’ENTER A TEMPERATURE IN DEGREES F:’ READ*,TEMPIN! example: read in TEMPIN = 82. ! Convert using function f_to_c TEMP =f_to_c(TEMPIN) ! Sends the value 82. to the function to be converted ! PRINT*,TEMPIN,TEMP! Prints 82. and 28. (try it!) ! END PROGRAM TEMPCON 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 13

Using sub-programs So: 1. YOU get to decide which pieces of code to assign to a function. 2. YOU get to write the code for that function. 3. YOU get to put code in the MAIN PROGRAM which gets information from the function. And: 4. YOU get to decide where to put the “function code”. 5. ??? 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 14

Using sub-programs There are two places we can put the “function code”: 1. At the end of the main code/after the main code. 2. At the end of the main code/inside the main code. Going back to our TEMPCON and f_to_c example… 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 15

Using sub-programs 1. At the end of the main code/after the main code. PROGRAM TEMPCON IMPLICIT NONE REAL :: TEMPIN, TEMP PRINT*,’ENTER A TEMPERATURE IN DEGREES F:’ READ*,TEMPIN TEMP =f_to_c(TEMPIN) PRINT*,TEMPIN,TEMP END PROGRAM TEMPCON FUNCTION f_to_c (Temperature) ! Input: Temperature = temp in deg F ! Output: f_to_c = temp in deg C IMPLICIT NONE REAL :: f_to_c, Temperature f_to_c = (Temperature )*9./5. RETURN END FUNCTION f_to_c 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 16

Using sub-programs 2. At the end of the main code/inside the main code. PROGRAM TEMPCON IMPLICIT NONE REAL :: TEMPIN, TEMP PRINT*,’ENTER A TEMPERATURE IN DEGREES F:’ READ*,TEMPIN TEMP =f_to_c(TEMPIN) PRINT*,TEMPIN,TEMP CONTAINS ← new KEYWORD FUNCTION f_to_c (Temperature) ! Input: Temperature = temp in deg F ! Output: f_to_c = temp in deg C IMPLICIT NONE REAL :: f_to_c, Temperature f_to_c = (Temperature )*9./5. RETURN END FUNCTION f_to_c END PROGRAM TEMPCON 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 17

Using sub-programs 3. Main code could look like: PROGRAM MAIN IMPLICIT NONE REAL :: bla bla bla whatever CONTAINS ← new KEYWORD FUNCTION sub1 (X1) bla bla bla RETURN END FUNCTION sub1 FUNCTION sub2 (X2) bla bla bla RETURN END FUNCTION sub2 etc. END PROGRAM MAIN 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 18

Using sub-programs (B) Subroutines: With a function, we can only get ONE piece of information back. With a subroutine, we can get MULTIPLE pieces of information back  much more powerful. 11/10/2011 MET 50, FALL 2011, CHAPTER 6 PART 1 19