6 April, 2000 CS1001 Lecture 13 PRACTICE EXAM - revisit SUBPROGRAM FUNCTION.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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 Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
6 April, 2000 CS1001 Lecture 15 EXAM1 - results SUBPROGRAM - - revisit FUNCTION.
27 March, 2000 CS1001 Lecture 16 FUNCTIONS SUBROUTINES SCOPE MODULES EXTERNAL SUBPROGRAMS.
PSU CS 106 Computing Fundamentals II VB Subprograms & Functions HM 4/29/2008.
CS 201 Functions Debzani Deb.
Chapter 10 Modules and programming with subroutines.
Chapter 9 Modules and Programming with Functions.
Chapter 6: User-Defined Functions I
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -
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 6: 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.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:
CPS120: Introduction to Computer Science Decision Making in Programs.
Week 3 Let's review! Fundamental data types List-directed input/output.
CPS120: Introduction to Computer Science Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Procedural programming in Java Methods, parameters and return values.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Subroutines. Subroutine Subprograms Common features of Subroutines and functions –They perform particular tasks under the control of some other program.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
CS314 – Section 5 Recitation 9
Information and Computer Sciences University of Hawaii, Manoa
Predefined Functions Revisited
Procedures and Modular Programming
A Lecture for the c++ Course
Organization of Programming Languages
User-Defined Functions
Midterm Review Programming in Fortran
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
COMPUTER PROGRAMMING SKILLS
Predefined Functions Revisited
Classes, Objects and Methods
DATA TYPES AND OPERATIONS
REPETITION Why Repetition?
Presentation transcript:

6 April, 2000 CS1001 Lecture 13 PRACTICE EXAM - revisit SUBPROGRAM FUNCTION

6 April, 2000 PROGRAM question1 REAL :: r, circum, area, diameter REAL, PARAMETER :: pi= CHARACTER(1) :: char PRINT *, 'Enter radius' READ *, r PRINT *, 'Enter C, A, or D' READ *, char IF (char.EQ.'C') THEN circum = 2 * pi * r PRINT *, 'circumference = ', circum ELSE IF (char.EQ. 'A') THEN area = pi * r**2 PRINT *, 'area = ', area ELSE IF (char.EQ. 'D') THEN diameter = 2 * r PRINT *, 'diameter = ', diameter ENDIF END PROGRAM question1

6 April, 2000 Practice exam # 2 IF (x.LE. 200) THEN IF (x.LT. 100) THEN IF (x.LE. 0) THEN print *, 'A' ELSE print *, 'B' ENDIF ELSE print *, 'C' ENDIF ELSE print *, 'D' ENDIF If (x < 100) If (x >= 100 ) If (x <=200) x >= 100.AND. x <= <= X <= 200

6 April, 2000 Practice Exam #3 assign a value of.TRUE. to EvenNumber if M is an even number; otherwise assign a value of.FALSE. EvenNumber = (M - M/2*2).EQ. 0 or EvenNumber = MOD (M,2).EQ. 0

6 April, 2000 Practice Exam #4 IF ((First.EQ. 'A').OR. (Last.EQ. 'Z')) THEN IF (Second.EQ. 'B') THEN Answer =.TRUE. ELSE Answer =.FALSE. ENDIF ELSE Answer =.TRUE. ENDIF = COND Second.EQ. 'B' COND =.TRUE. COND =.FALSE. (COND.AND. (Second.EQ. ‘B’)).OR. ( COND =.FALSE.) (COND.AND. (Second.EQ. ‘B’)).OR..NOT. COND Other answers are possible

6 April, 2000 Function and Subroutine Subprograms -- each is like a miniature program Function consists of: FUNCTION heading Specification part Execution part END FUNCTION statement Subroutine consists of: SUBROUTINE heading Specification part Execution part END SUBROUTINE statement

6 April, 2000 Function Table 6.1 (p. 324) and Appendix A list Fortran library functions Programmer defined Functions –separate program units that perform some function(s) –included with main program –invoked at in assignment statements in the flow of the program –communicates via passing parameters and returning result –should return a single value –must be assigned a type

6 April, 2000 Where to Put Function Code The Code for programmer defined functions are inserted at the end of the main program between a CONTAINS statement and the END PROGRAM PROGRAM ProgramName. CONTAINS Function(s) go here END PROGRAM ProgramName

6 April, 2000 PROGRAM Temperature_Table IMPLICIT NONE INTEGER :: iRange, Init, Limit,Step REAL :: Range, Fahr, CTOF DO iRange = Init, Limit, Step Range = REAL (iRange) Fahr = CTOF(Range) PRINT *, Range, Fahr END DO CONTAINS REAL FUNCTION CTOF(Cels) : END FUNCTION CTOF END PROGRAM Temperature_Table Good practice to declare function type in any program unit that calls the function

6 April, 2000 Function General Form FUNCTION function_name (formal_argument_list) type_identifier :: function-name (Declaration section) … (Execution section) … function_name = expr END FUNCTION function_name formal_argument_list is a list of identifiers (may be blank) type_identify is used to identify the type of the FUNCTION Alternate Heading: type_identifier FUNCTION function_name (formal_argument_list)

6 April, 2000 Converting With Function DO iRange = Init, Limit, Step Range = iRange Fahr = CTOF(Range) PRINT *, Range, Fahr END DO Where: FUNCTION CTOF(Cels) REAL :: CTOF REAL, INTENT(IN) :: Cels CTOF = 1.8 * Cels END FUNCTION CTOF

6 April, 2000 Flow of Control PROGRAM Temperature_Table... REAL FUNCTION CTOF (Cels)... CTOF = 1.8 * Cels END FUNCTION CTOF Range Fahr = CTOF(Range)... END PROGRAM Temperature_Table Range -- actual parameter Cels -- formal parameter

6 April, 2000 Voltage example PROGRAM ComputeVoltage REAL :: Time, Volts, Voltage... Time = 2.5 Volts = Voltage (Time)... CONTAINS FUNCTION Voltage(Time) REAL :: Voltage REAL, INTENT(IN) :: Time Voltage = (Time + 0.1)* EXP(SQRT(Time)) END FUNCTION Voltage END PROGRAM ComputeVoltage Given program statement. Function Voltage is referenced with an argument of Time = 2.5 Expression is evaluated as: ( ) * EXP(SQRT(2.5)) = Volts is then set equal to the computed value of

6 April, 2000 Function Equivalence Following are the same Time = 2.5 Volts = Voltage(Time) Time = 2.5 Volts = (Time + 0.1) * EXP(SQRT(Time)) If the computation is going to be done multiple times within a program (not within a loop), it pays to write a function. Voltage is a FUNCTION

6 April, 2000 Argument (Parameter) Passing Caller ( program or subprogram) function_name (actual_argument_list) function_name (formal_argument_list) Callee Each actual argument is associated with the corresponding formal argument -- must agree in number and type Used to pass values

6 April, 2000 Argument Passing Arguments are used to pass information between (to/from) caller and callee. INTENT Specifier Tells how the arguments are to transfer information – type, INTENT(IN) :: argument for inputs TO either a function or subroutine – type, INTENT(OUT) :: argument for outputs FROM a function or subroutine (but not good practice to have OUT arguments in function) – type, INTENT(INOUT) :: argument for both TO and FROM a subprogram

6 April, 2000 INTENT (IN) specification Used to ensure –value of the actual argument is passed to the formal parameter –the value of the formal argument cannot be changed while the function is being executed Well designed Fortran function –produce a single output value from one or more input values –never modify its own input arguments -- always declare the arguments with INTENT(IN) attribute.

6 April, 2000 Unintended side effects Changing the value of a variable in one part of the program affects, unintentionally, the value of that variable or other variables in other parts of the program. Often dangerous because –they might cause wrong results –are hard to debug (therefore, wrong results might go unnoticed.)

6 April, 2000 Old FORTRAN IV Example In calling program Sum = 0 Sum = addsum (10)... Sum = Sum + 10 Print *, Sum In Function addsum (IMAX)... IMAX = 5 addsum =... (say 20) END Name value in storage Sum 010 IMAX10, then Name value in storage Sum Name value in storage Sum Name value in storage Sum After 5 At 6 Print Sum will give 25 2

6 April, 2000 Scope The portion of the program in which an entity (variable, constant, subprogram, types) is visible, i.e., where it is accessible and can be used. Fundamental 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.