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.

Slides:



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

User Defined Functions
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
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.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Functions Jay Summet. Aug Functions A function is a piece of code you can use over and over again Treat it like a black box You pass it (optional)
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions.
6 April, 2000 CS1001 Lecture 15 EXAM1 - results SUBPROGRAM - - revisit FUNCTION.
27 March, 2000 CS1001 Lecture 16 FUNCTIONS SUBROUTINES SCOPE MODULES EXTERNAL SUBPROGRAMS.
Basic Building Blocks In all ways of life, the easiest way to solve most problems is to break them down into smaller sub_problems and then to deal with.
CS 201 Functions Debzani Deb.
Chapter 10 Modules and programming with subroutines.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
Chapter 9 Modules and Programming with Functions.
Digital System Design Verilog ® HDL Tasks and Functions Maziar Goudarzi.
6 April, 2000 CS1001 Lecture 13 PRACTICE EXAM - revisit SUBPROGRAM FUNCTION.
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.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Tasks and Functions Programmable Logic Design (40-493) Fall 2001 Computer Engineering Department Sharif University of Technology Maziar Gudarzi.
Fortran: Specification Statements Session Six ICoCSIS.
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
Fortran: Program Units and Procedures Session Four ICoCSIS.
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
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Week 8. Today’s program n New stuff: –Recursion in F –Procedures as arguments –User-defined data types n Quiz #3.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Week 8 Improving on building blocks Recursive procedures.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
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:
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
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.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Week 4: Organizational matters Register yourself to CIMS (Course Information and Management System) by clicking on Homework at hidiv.cc.itu.edu.tr/~F90.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
ME 142 Engineering Computation I Using Subroutines Effectively.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Subroutines. Subroutine Subprograms Common features of Subroutines and functions –They perform particular tasks under the control of some other program.
ME 142 Engineering Computation I Using Subroutines Effectively.
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.
Fortran MATTHEW CARSON. History of FORTRAN FORTRAN ; First High Level Language FORTRAN I 1957; First Compiled Language FORTRAN II ; Independent.
Chapter 3: User-Defined Functions I
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Oracle11g: PL/SQL Programming Chapter 5 Procedures.
Procedures and Modular Programming
Functions in C Mrs. Chitra M. Gaikwad.
Subroutine Comp 208 Yi Lin.
User Defined Functions
Midterm Review Programming in Fortran
G. Pullaiah College of Engineering and Technology
mdul-cntns-sub-cmmnts2.f90
In C Programming Language
Introduction to Problem Solving and Programming
CPS125.
Top-Down Design with Functions
Presentation transcript:

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 case – the program that uses the function must have an INTERFACE block

Syntax interface type function name(arg-1,arg-2,…,arg-n) type, intent (in) :: arg-1 type, intent (in) :: arg type, intent (in) :: arg-3 end function name... other functions... end interface

Placing Interface Block ► After IMPLICIT NONE

Illustration ► Celsius and Fahrenheit Conversion ► Cm and Inch Conversion

Subroutines ► Program unit designed to perform a particular task ► Receives some input via formal arguments ► Returns the results (if any) with some formal arguments

Function vs. Subroutines ► Functions – designed to return a single value ► Subroutines – often return more than one value, even may return no value at all

Function vs. Subroutines Contd.. ► Functions – returns values via function name ► Subroutines – returns values via arguments

Function vs. Subroutines Contd.. ► Functions – referenced by using its name in an expression ► Subroutines – referenced by a CALL statement

Syntax – Form 1 subroutine subroutine_name (arg-1,…,arg-n) implicit none [specification part] [execution part] end subroutine subroutine_name

Syntax – Form 2 subroutine subroutine_name () implicit none [specification part] [execution part] end subroutine subroutine_name

Syntax – Form 3 subroutine subroutine_name implicit none [specification part] [execution part] end subroutine subroutine_name

Declaration of Arguments ► Argument that receives value from outside – declared with intent(in) ► Argument that does not receive value brut passes a computation to the outside – declared with intent(out ) ► Argument that does both – declared with intent(inout )

An example subroutine swap(a,b) implicit none integer, intent(inout) :: a,b integer :: temp temp=aa=bb=temp end subroutine swap

CALL Statement - Syntax CALL subroutine_name (arg-1,…,arg-n) or CALL subroutine_name() or CALL subroutine_name

Main Program program swapnum implicit none integer :: a,b a=5b=8print*,a*10+b call swap(a,b) print*,a*10+b end program swapnum

Module ► Parameters, variables, subprograms should be shared by separate programs ► Module – package of declarations and definitions; can be imported into other programs

Simplest Form module module-name implicit none [specification part] end module module-name

Illustration module circle implicit none real, parameter :: pi = real :: radius end module circle

Main Program program process_circle use circle implicit none print*,'radius?'read*,radiusprint*,area(radius)contains real function area(radius) implicit none real, intent(in) :: radius area=pi*radius**2 end function area end program process_circle

Module may also contain procedures module circle implicit none real, parameter :: pi = real :: radius contains real function area(radius) implicit none real, intent(in) :: radius area=pi*radius**2 end function area end module circle

New Main Program program process_circle use circle implicit none print*,'radius?'read*,radiusprint*,area(radius) end program process_circle