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.

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

Procedural programming in Java
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Fall 2008Programming Development Techniques 1 Topic 2 Scheme and Procedures and Processes September 2008.
Chapter 6 Programming with Functions. FUNCTIONS Intrinsic Functions (or called library functions) Function Subprograms: programmer-defined functions.
Chapter 6: User-Defined Functions I
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
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.
27 March, 2000 CS1001 Lecture 16 FUNCTIONS SUBROUTINES SCOPE MODULES EXTERNAL SUBPROGRAMS.
Chapter 9 Modules and Programming with Functions.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
6 April, 2000 CS1001 Lecture 13 PRACTICE EXAM - revisit SUBPROGRAM FUNCTION.
Chapter 9 Introduction to Procedures Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul -
Summary of previous weeks BIL 102 Introduction to Scientific & Engineering Computing.
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.
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.
Fortran: Specification Statements Session Six ICoCSIS.
Fortran: Program Units and Procedures Session Four ICoCSIS.
Munster Programming Training
Program readformat integer::n1,n2,n3 real::x,y,z print*,"Please enter " read"(3i3)",n1,n2,n3 print*,"The format for read this data is 3i3" print*,"n1=",n1,"n2=",n2,"n3=",n3.
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 Improving on building blocks Recursive procedures.
Functions. Type of Subprograms Fortran 90/95 allows for two types of subprograms: –Functions, and –Subroutines. In general, there are two forms of subprograms:
Week 3 Let's review! Fundamental data types List-directed input/output.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
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.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Week 4: Organizational matters Register yourself to CIMS (Course Information and Management System) by clicking on Homework at hidiv.cc.itu.edu.tr/~F90.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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.
1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose.
FUNCTIONS IN FORTRAN For a complex program difficulties like writing it and debugging are encountered. These can be minimized by breaking main program.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
CS Algorithm Development Modularity. Objectives Do “high-level design” Practice abstraction by decomposing problems into their sub-tasks Define.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Procedures and Modular Programming
Chapter 5 Controlling the flow of your program
Organization of Programming Languages
CSCI 161: Introduction to Programming Function
User-Defined Functions
Functions Declarations CSCI 230
CPS125.
Presentation transcript:

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 each of these in turn, further sub-dividing these subproblems as necessary.

Seven Golden Rules Always plan ahead Develop in stages Modularize Keep it simple Test throughly Document all programs Enjoy your programming

Programs and modules Main program unit program name use statements. Specification statements. Executable statements. end program name

Modules  Programs for solving complex problems should be designed in a modular fashion.  The problem should be divided into simpler subproblems, so that the subprograms can be written to solve each of them.  Every program must include exactly one main program and may also include one or more modules.

Modules Modules are a second type of program unit. The basic structure of a module is similar to the main program unit. The initial module statement of each module specifies the name of that module based on the F language rules. A module unit ends with an end module statement incuding its name. A module does not contain any executable statements. A module may contain any number of subprograms which are seperated from the other statements by a contain statement.

Module program unit module name use statements. Specification statements. contains (Procedure definitions) subprogram_1 subprogram_2. subprogram_n end module name

Procedures A special section of program which is, in some way, referred to whenever required, is known as a “procedure”. Programs  can be written by the programmer  by some other person who allows the programmer to use them  can be a part of the F language itself (i.e. intrinsic procedures whose names are reserved words  must always be written in lower case). Subprograms can also be categorized as subroutines ( there are 5 intrinsic subroutines ) functions ( create only a single result ; there are 97 intrinsic functions available in F )

Procedures Divide and rule! Main program Procedure 1 Procedure 2 Procedure 3

Procedures Procedures - origin “Write your own” (homemade) Intrinsic (built-in, comes with F ) sin(x), cos(x), abs(x), … Written by someone else (libraries) Procedures (subprograms) – form Functions Subroutines

Procedures The main program and any subprogram need never be aware of the internal details of any other program or subprogram! Main program Procedure 1 Procedure 2 Procedure 3

Procedures name (argument_1, argument_2,...) Examples: a + b * log (c) -b + sqrt ( b * b – 4.0 * a * c)

Procedures A) PROBLEM : A farmer has a triangular field which he wishes to sow with wheat. Write a program that reads the lenghts of the 3 sides of the field (in meters) and the sowing density (in grams per square meters) Print the number of 10 kilo bags of wheat he must purchase in order to sow the whole field. B.) ANALYSIS : STRUCTURE PLAN of the PROBLEM read lenghts of the sides of the field ( a, b, c ) and calculate the area of the field area = ( s (s-a)(s-b)(s-c) ) ½ 2s = a + b + c read the sowing density calculate the quantity of wheat seed required calculate the number of 10 kilo bags this represents

C) SOLUTION program wheat_sowing ! This program calculate quantity of wheat required to sow a triangular field ! Variable declarations real : : a, b, c,s, area, density, quantity integer : : num_bags ! read the lengths of the sides of the field print *, “type the lengths of the 3 sides of the field in metres : “ read *, a, b, c ! calculate the area of the field s = 0.5 * ( a + b + c ) area = sqrt ( s * (s - a)*(s - b)*(s - c) ) ! read sowing density print *, “ What is the sowing density (gms/sq.m) ?” read *, density ! calculate quantity of wheat and the number of 10 kilo bags ! round up more than 1 kg quantity = density * area num_bags = * quantity ! print results print *, “the area of the field is “, area,” sq. metres” print *, “and “, num_bags,” 10 kilo bags will be required” end program wheat_sowing

Subprograms Functions : Functions may, of course, be provided by the user and they are normally implemented by means of an F subprogram which is physically placed within a module as is explained in Modules. On the other hand, very important principle which applies to all procedures in F is that the main program and any subprograms need never be aware of the internal details of any other program unit or subprograms. A function subprogram can be called by  the main program  another subroutine subprogram  another function

Functions function name (d1, d2, …) result (result_name) Specifications part. Execution part end function name Variables Internal (local) variables Result variable (keyword result ) Dummy argument (keyword intent(in) ) attribute

Functions function cube_root result(root) ! A function to calculate the cube root of ! a positive real number ! Dummy argument declaration real, intent(in) :: x ! Result variable declaration real :: root ! Local variable declaration real :: log_x ! Calculate cube root by using logs log_x = log(x) root = exp(log_x/3.0) end function cube_root

Subroutines subroutine roots (x, square_root, cube_root, fourth_root, & fifth_root) ! Subroutine to calculate various roots of positive real ! Number supplied as the first argument, and return them in ! the second to fifth arguments ! Dummy argument declarations real, intent(in) :: x real, intent(out) :: square_root, cube_root, & fourth_root, fifth_root ! Local variable declaration real :: log_x ! Calculate square root using intrinsic sqrt square_root = sqrt(x) ! Calculate other roots by using logs log_x = log(x) cube_root = exp(log_x/3.0) fourth_root = exp(log_x/4.0) fifth_root = exp(log_x/5.0) end subroutine roots

Subroutines call name (arg1, arg2, …) intent(in), intent(out), intent(inout)

Arguments Actual arguments in the calling program Dummy arguments in the subroutine or function The order and types of the actual arguments must correspond exactly with the order and types of the corresponding dummy arguments

Objects Local variables vs. global variables private vs. public objects

Saving the values of local objects Local entities within a procedure are not accessible from outside that procedure Once an exit has been made, they cease to exist If you want their values to ‘survive’ between calls, use real, save :: list of real variables real, save::a, b=1.23, c Integer, save::count=0

Example MAIN PROGRAM program …….. real : : Alpha, Beta, Gamma. Alpha = Fkt ( Beta, Gamma ). end program ………. FUNCTION SUBPROGRAM function Fkt ( x, y ) real : : Fkt real : : x, y Fkt = x ** * y * y ** 2 x = 0.0 end function Fkt

Example: Write a subprogram which calculates the cube root of a positive real number MAIN PROGRAM program test_cube_root use maths real : : x print *, “Type a positive real number” read *, x Print *, “ The cube root of “,x,” is “, cube_root(x). a = b * cube_root(x) + d. end program test_cube_root

module maths Public:: cube_root contains function cube_root (x) result (root) ! a function to calculate the cube root of a positive real number ! Dummy arguments real, intent (in) : : x ! Result variable declaration real : : root ! Local variable declaration real : : log_x ! Calculate cube root by using logs log_x = log (x) root = exp (log_x / 3.0) function cube_root end module maths

Attributes intent (in): the dummy argument only provides information to the procedure and is not allowed to change its value any way intent (out): the dummy argument only returns information from the procedure to the calling program intent (inout): the dummy argument provides information in both directions

Examples of subprograms Write a program using either subroutine or function: read the edges of a rectangle, write a subprogram which calculate area of rectangle

!this program is calculates area of a rectangle !using subroutine program area_calculation use rec real::a,b,al print *, "enter two edges of the rectangle" read *, a,b call area (a,b,al) print *, "a=",a print*,"b=",b print *, "area_of_rectangle=",al endprogram area_calculation module rec public::area contains subroutine area(a,b,al) real, intent(in)::a,b real, intent (out)::al al=a*b return endsubroutine area end module rec programsubprogram

!this program is calculates area of a rectangle program area_calculation use rec real::a,b,al print *, "enter two edges of the rectangle" read *, a,b al=area (a,b) print *, "a=",a print*,"b=",b print *, "area_of_rectangle=",al endprogram area_calculation module rec public::area contains function area(a,b)result (al) real, intent(in)::a,b real::al al=a*b return endfunction area end module rec programsubprogram

Write a program using either subroutine or function: n read the three edges of a triangle, n write a subprogram which calculate area of triangle

!this program calculate area of a triangle !using subroutine program triangle_area use ak real :: a,b,c,al print *,"a,b,c" read *,a,b,c call alan(a,b,c,al) print *,al end program triangle_area module ak public :: alan contains subroutine alan(a,b,c,al) real, intent (in)::a,b,c real, intent (out)::al real :: s s=(a+b+c)/2 al=sqrt(s*(s-a)*(s-b)*(s-c)) return end subroutine alan end module ak programsubprogram

programsubprogram program triangle_area use ak real :: a,b,c,al print *,"a,b,c" read *,a,b,c al= alan(a,b,c) print *,al end program triangle_area module ak public :: alan contains function alan(a,b,c) result (al) real, intent(in) :: a,b,c real :: al real :: s s=(a+b+c)/2.0 al=sqrt(s*(s-a)*(s-b)*(s-c)) return end function alan end module ak

HOMEWORK 2 Dead Line : 28 / 03 / 2001 PROBLEM Consider a geometrical body which consists of a steel- cylinder and a cone of lead having the same radius. Then, prepare a computer program having the structure described in below :

The main program will: read the input values (radius r=50cm, height of the cylinder, h1=300cm, height of the cone h2 = 150cm, density of steel, d1 = 7.85 t/m 3, density of lead d2 = t/m 3 ) calculate the total volume of the given body calculate the total weight of the given body finally print,  the input values  the volume of the cylinder  the weight  the volume of the cone  the weight of the cone  the total volume of the given body  the total weight of the given body using list directed output command including the above-given expressions for the input values and the results obtained.

A function sub-program will calculate only the bottom surface area A subroutine sub-program will calculate the volume of the cylinder the weight of the cylinder Another subroutine sub-program will calculate the volume of the cone the weight of the cone