27 March, 2000 CS1001 Lecture 16 FUNCTIONS SUBROUTINES SCOPE MODULES EXTERNAL SUBPROGRAMS.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
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 Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
17 March, 2000 CS1001 Lecture 2 Programming and problem solving Software engineering practices.
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 7: User-Defined Functions II.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Semantics of Calls and Returns
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Chapter 6: User-Defined Functions I
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
6 April, 2000 CS1001 Lecture 13 PRACTICE EXAM - revisit SUBPROGRAM FUNCTION.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Chapter 6: Functions.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Fortran: Specification Statements Session Six ICoCSIS.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Module and Data Sharing. Programming in the Large Software, in general, is large having multiple units Multiple units designed and developed independently.
Week 3 Let's review! Fundamental data types List-directed input/output.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Programming Fundamentals Enumerations and Functions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
CSCI 161 Lecture 3 Martin van Bommel. Operating System Program that acts as interface to other software and the underlying hardware Operating System Utilities.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Lecture 3 Translation.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
A Lecture for the c++ Course
CSC113: Computer Programming (Theory = 03, Lab = 01)
User-Defined Functions
Chapter 7: User-Defined Functions II
Presentation transcript:

27 March, 2000 CS1001 Lecture 16 FUNCTIONS SUBROUTINES SCOPE MODULES EXTERNAL SUBPROGRAMS

27 March, 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)

27 March, 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

27 March, 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.

27 March, 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.)

27 March, 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

27 March, 2000 SUBROUTINE General Form SUBROUTINE Subroutine_name (formal_argument_list) (Declaration section) … (Execution section) … subroutine_name = expr END SUBROUTINE subroutine_name formal_argument_list is a list of identifiers (may be blank)

27 March, 2000 Subroutine Reference Each subroutine should have a header. The program using the subroutine does not declare the subroutine. The subroutine is referenced via a CALL statement The code for the subroutine follows the main program CONTAINS statement and precedes the END PROGRAM statement

27 March, 2000 Subroutine Example SUBROUTINE DDMMSS(Angle) REAL, INTENT(IN) :: Angle INTEGER :: Deg, Min, Sec Deg = INT(Angle) Min = INT(Angle * 60) - (Deg * 60) Sec = INT(Angle * 3600) - (Deg * & 3600) - (Min * 60) PRINT *, Angle, ‘ degrees = ‘ PRINT *, Deg, ‘ deg ‘, Min, ‘ min& & ‘, Sec, ‘ sec’ END SUBROUTINE DDMMSS

27 March, 2000 Subroutine Calling Within a program, the subroutine is called as: CALL DDMMSS(Input) At this point in the program execution, the subroutine DDMMSS is called with angle Input as an argument, causing the subroutine to execute and display the angle in DD MM SS format

27 March, 2000 Subroutine Argument Return SUBROUTINE DDMMSS(Angle, DDD, MM, SS) REAL, INTENT(IN) :: Angle INTEGER, INTENT(OUT) :: DDD, MM, SS DDD = INT(Angle) MM = INT(Angle * 60) - (DDD * 60) SS = INT(Angle * 3600) - (DDD * & 3600) - (MM * 60) END SUBROUTINE DDMMSS

27 March, 2000 Subroutine Argument Return The program would include the following statements: CALL DDMMSS(Input, Deg, Min, Sec) PRINT *, Input, ‘ degrees = ‘ PRINT *, Deg, ‘ deg ‘, Min, ‘ min ‘, Sec, ‘ sec‘ Variables DEG, MIN, and SEC are passed to the subroutine to store the computed values for those variables Input Angle Deg DDD Min MM Sec SS Actual arguments Formal arguments

27 March, 2000 Converting With Subroutine DO iRange = Init, Limit, iStep Range = iRange CALL CTOF(Range, Fahr) PRINT *, Range, Fahr END DO Where: SUBROUTINE CTOF(Cel, Far) REAL, INTENT(IN) :: Cel REAL, INTENT(OUT) :: Far Far = 1.8 * Cel END SUBROUTINE CTOF Fahr = CTOF(Range) FUNCTION CTOF(Cels) REAL :: CTOF REAL, INTENT(IN) :: Cels CTOF = 1.8 * Cels END FUNCTION CTOF

27 March, 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.

27 March, 2000 Scope examples PROGRAM MAIN INTEGER :: X, Y : CONTAINS SUBROUTINE SUB1(…) REAL :: Y : END SUBROUTINE SUB1 SUBROUTINE SUB2(…) INTEGER:: X : END SUBROUTINE SUB2 END PROGRAM MAIN Y of SUB1 X of MAIN X of SUB2 Y of MAIN

27 March, 2000 SAVE attribute Values of local variables in a subprogram are not retained from one execution to the next A variable in a subprogram can retain value if –initialized in the declaration –have SAVE attribute type, SAVE :: list_of_local_variables if list_of_local_variables is omitted, all local variables are saved –For example: INTEGER FUNCTION ADD(…) INTEGER :: COUNT = 0, SUM=0 INTEGER, SAVE :: RUNING_TOTAL : END FUNCTION ADD

27 March, 2000 MODULES A Module –is a program unit to package together type declarations, subprograms, and definitions of new data types –provides a way to bundle a library of useful code to be used in other program units Form: MODULE module_name CONTAINS subprogram1 subprogram2 : type_declarations END MODULE module_name E.g., REAL, PARAMETER:: PI=

27 March, 2000 Modules Modules, once defined, can be used in a programming unit by a USE statement For Example, PROGRAM Complex_Calc USE complex_functions : END PROGRAM Complex_Calc

27 March, 2000 Preparing a Program for Execution You enter the program and save it as a source file The compiler attempts to translate the program You correct syntax error The linker links the new object file with other object files The loader places the load file into memory Executable program in memory Load File Source file on disk New Object File Other Object File Revised Source file List of errors Oops! Good job!

27 March, 2000 Linking Programs and Modules Program source file Module source file Program Object file Module Object file F90 Compiler Linker Executable File

27 March, 2000 External Subprograms Subprogram attached after the END PROGRAM statement Separate and independent from main program No access to each others local variables Information sharing through function name and arguments Use Interface blocks to ease compatibility checking INTERFACE Interface_body END INTERFACE

27 March, 2000 Internal vs External Subprograms -Forms Internal: PROGRAM Main type :: function_name : expr = function_name (..) call subroutine_name (..) : INCLUDES type FUNCTION function_name (..) : function_name = expr1 END FUNCTION function_name SUBROUTINE subroutine_name : END SUBROUTINE subroutine_name END PROGRAM Main External: PROGRAM Main INTERFACE FUNCTION function_name (..) type :: function_name type :: arguments_list END FUNCTION function_name END INTERFACE : expr = function_name (..) : END PROGRAM Main FUNCTION function_name (..) type :: function_name type :: arguments_list : END FUNCTION function_name

27 March, 2000 Module - Form Module MODULE module_name INCLUDES type FUNCTION function_name (..) : function_name = expr1 END FUNCTION function_name SUBROUTINE subroutine_name (..) : END SUBROUTINE subroutine_name : END MODULE module_name PROGRAM Main : USE MODULE module_name : expr = function_name (..) CALL subroutine_name (..) : END PROGRAM Main