7 - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

7

Description:

Example for usage of intrinsic subroutines: program Date_and_time_example ... Example for function: Write a function to calculate the cube root of a positive ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 40
Provided by: nuzhet
Category:
Tags:

less

Transcript and Presenter's Notes

Title: 7


1
7 8 PROGRAMMING WITH FUNCTIONS SUBROUTINES
  • Ellis Philips, 1998, p 69-102

2
Programs and Modules
  • A sequence of statements starting with a program
  • statement and ending with an end program
    statement is
  • formally known as a main program or main program
    unit.
  • Between these two statements there are 3 main
    groups of
  • statements, namely use statements as a means of
    accessing
  • modules, specification statements and executable
  • statements.

3
A main program unit
  • program name
  • use statements
  • .
  • specification statements
  • .
  • executable statements
  • .
  • end program name

4
  • F also has a second type of program unit, called
    a module.
  • Module includes a set of related objects
    procedures,
  • variables, data types and makes them available to
    a
  • program in a controlled manner.

5
A module program unit
  • module name
  • use statements
  • .
  • .
  • specification statements
  • end module name
  • A module may not contain contain any executable
  • statements.

6
A module program unit containing procedure
definitions
  • module name
  • use statements
  • .
  • specification statements
  • .
  • contains
  • procedure definitions
  • .
  • .
  • end module name
  • It may contain any number of subprograms which
    are
  • separated from the other statements in the module
    by
  • a contains statement.

7
Procedures
  • F procedures may be subroutines or functions.
  • Procedures can be divided into two categories
    which are
  • written by the programmer and (external
    procedures)
  • those which are part of the F language itself
    (intrinsic
  • procedures).
  • Other categories based on their mode of use are
  • called functions and subroutines.

8
Intrinsic functions
  • F contains a number of intrinsic functions for
    elementary
  • mathematical functions such as
  • sin(x) where x is in radians
  • log(x) logex
  • sqrt(x)
  • abs(x)
  • See Appendix A, p 591-600
  • Example ablog(x)

9
Intrinsic subroutines
  • F contains five intrinsic subroutines such as
  • date_and_time
  • call date_and_time (date, time, zone)
  • Date ccyymmdd (century,year,month,day)
  • Time hhmmss.sss (hours,minutes,seconds and
    miliseconds)
  • Zone ?hhmm (difference between the local time
    zone and GMT)

10
Example for usage of intrinsic subroutines
  • program Date_and_time_example
  • character (len10) date, time, time_zone
  • call date_and_time (date,time,time_zone)
  • print ,The date is,date(78), /,date(56),
    /,date (14)
  • print ,The time is,time(12), h,time(34),
  • m,time_zone, time_zone (5)
  • end program Date_and_time_example

11
FUNCTIONS
  • A function is given information to operate on by
    means of
  • one or more arguments and delivers a single
    result.
  • function name (d1,d2,..) result (result_name)
  • ..
  • end function name
  • d1,d2 are dummy arguments representing the actual
  • arguments.
  • result_name is the name of a variable which will
    be used to
  • store the result of the function.

12
Example for function Write a function to
calculate the cube root of a positive real
numbers
  • function cube_root (x) result (root)
  • ! A function to calculate the cube root of a
    positive
  • ! real number
  • real, intent (in) x
  • real root
  • real log_x
  • log_xlog(x)
  • rootexp(log_x/3.0)
  • end function cube_root

13
Explanation of the example for function
  • Internal variables or local variables
  • log_x root are not accessible from outside the
    function
  • Result variable
  • root is the result variable appears in
    result statement.
  • Decleration of the dummy arguments
  • real, intent (in) x
  • The dummy argument x may not be changed by the
    function cube_root.
  • end function
  • It causes execution of the program to return to
    the point in the calling program.

14
MODULE
  • Procedures in F must always be defined in a
    module.
  • The purpose of a module is to group together
    various entities and to make
  • them available in a controlled fashion to
    programs and procedures that
  • require access to them. Every procedure that is
    to be made accessible in
  • this way must appear in a public statement which
    takes the form
  • public list of names
  • gtgt
  • module maths
  • public cube_root
  • contains
  • function cube_root (x) result (root)
  • .
  • end function cube_root
  • end module maths
  • gtgt

15
  • Any program or procedure that wishes to use the
    cube_root
  • function must contain a use statement
    referencing the
  • maths module.
  • use module_name
  • gtgt
  • program test_cube_root
  • use maths
  • real x
  • print ,Type a positive number
  • read ,x
  • print ,The cube root of ,x,is,cube_root(x)
  • end program test_cube_root
  • gtgt

16
  • Execution of a function is initiated by the
    appearance of the function name in an expression.
  • A function is not permitted to call a subroutine
    either directly or indirectly.
  • A function must not refer to itself either
    directly or indirectly.

17
SUBROUTINES
  • A subroutine arguments are used both to receive
  • information to operate on and to return results.
  • A subroutine is accessed by means of a call
    statement which
  • gives the name of the subroutine and a list of
    arguments
  • which will be used to transmit information
    between the calling
  • program unit and subroutine.
  • call name (arg1,arg2,)
  • Unlike a function, the values are returned by
    means of one
  • or more of its arguments.

18
Example for subroutine Write a subroutine,
roots, which calculates the square root, the cube
root, the fourth root and the fifth root of a
positive real number.
  • subroutine roots (x, square_root, cube_root,
    fourth_root, fifth_root)
  • real intent(in) x
  • real, intent(out) square_root, cube_root,
    fourth_root, fifth_root
  • real log_x
  • square_rootsqrt (x)
  • log_xlog (x)
  • cube_rootexp(log_x/3.0)
  • fourth_rootexp(log_x/4.0)
  • fifth_rootexp(log_x/5.0)
  • end subroutine roots

19
Calling subroutine
  • program subroutine_demo
  • use example_module
  • real pos_num,root_2,root_3,root_4,root_5
  • print ,Please type a positive real number
  • read ,pos_num
  • call roots (pos_num, root_2, root_3, root_4,
    root_5)
  • print ,The square root of,pos_num,is,root_2
  • print ,The cube root of,pos_num,is,root_3
  • print ,The fourth root of,pos_num,is,root_4
  • print ,The fifth root of,pos_num,is,root_5
  • end program subroutine_demo

20
  • If a subroutine has no arguments, then its
    initial statement takes the form
  • subroutine sub ()
  • call sub ()

21
ACTUAL DUMMY ARGUMENTS
  • call sub (a,c,d)
  • subroutine sub (x,y,z)
  • a,c,d are the actual arguments.
  • x,y,z are the dummy arguments.
  • The order and types of the actual arguments must
  • correspond exactly with the order and types of
  • the corresponding dummy arguments.

22
The intent attribute must be used to control
the direction in which arguments are used to pass
information
  • intent (in) dummy argument provides information
    to procedure and it is not be altered by
    procedure.
  • intent (out) dummy argument returns information
    from the procedure to the calling program.
  • intent (inout) dummy argument may be used for
    transmission of information in both directions.
  • A functions dummy arguments must all have
    intent(in).
  • Subroutines arguments may have any of the three
    forms
  • of intent attribute.

23
  • A character dummy argument must be declared with
    an
  • assumed length that is (len).
  • character (len), intent(in) name
  • Assumed length character declaration can only be
    used for
  • declaring a dummy argument and involves replacing
  • the length specifier by an asterisk.

24
LOCAL GLOBAL OBJECTS
  • Each program unit is only aware of any variables
    declared within the program unit itself referred
    to as its local variables together with
    pseudo-variables known as dummy arguments.
  • Modules are the pack of procedures. A module must
    contain public statement to specify the names of
    procedures that are to be made available to any
    program unit using the module and a similar
    concept using public attribute in the declaration
    statement can also be used to specify variables
    and named constants and some other entities that
    are to be made available to users of the module.
    Public statement must appear before any
    declaration statements.

25
Examples
  • gtgt
  • module natural_constants
  • real, parameter, public pi3.1415, e2.718
  • end module natural_constants
  • gtgt
  • gtgt
  • subroutine module_example (arg1,arg2)
  • use natural constants
  • .
  • end subroutine module_example
  • gtgt

26
Examples
  • module example
  • public sub1,sub2
  • real, parameter, public pi3.1415, e2.718
  • real, public global_var1, global_var2
  • contains
  • subroutine sub1 (a,b,c)
  • cpiab
  • end subroutine sub1
  • subroutine sub2 (x,y,z)
  • global_var1xglobal_var2
  • end subroutine sub2
  • end module example

27
  • If it is not required to access the constants or
    variables outside the module the public attribute
    may be replaced by private.
  • real, parameter, private pi3.1415, e2.718
  • real, private global_var1,global_var2
  • In this case the entities within the module will
    not be accessible from outside.
  • A private statement must appear after any use
    statements but before any other statements.
  • module another_module
  • use natural_constants
  • private
  • real, parameter, public pi_by_two0.5pi
  • .
  • end module another_module

28
  • module super_module
  • use module_1
  • use module_2
  • .
  • use module_n
  • public
  • end module super_module
  • A public statement which indicates that
    everything accessible from the individual modules
    is to be accessible from this module.

29
SAVING THE VALUES OF LOCAL OBJECTS ON EXIT FROM A
PROCEDURE
  • The values of procedures local variables may be
    retained between references by use of the save
    attribute.
  • real, save list of real variables
  • It is not possible to give the save attribute to
    a dummy argument or a result variable.
  • It is not permissible to specify the save
    attribute for any variables declared in the main
    program.

30
GIVING PROCEDURE VARIABLES ON INITIAL VALUE
  • Local variables in a procedure may be given an
    initial value
  • as a part of their declaration as long as they
    have save
  • attribute. It is not possible to use this form of
    initialization in
  • the main program since the save attribute can
    only be used
  • with local variables in a subroutine or function.
  • real, save a, b1.23, c
  • integer, save count0

31
  • Restrictions on the initialization expressions
  • - All elements of the expression must be
    constants or references to intrinsic functions
    with constant arguments.
  • Any exponentiation must be an integer power.
  • - Any named constants must have already been
    defined.

32
PROCEDURES AS AN AID TO PROGRAM STRUCTURE
  • Procedures provide the basic building blocks for
    modular development and top-down program design.
  • A modular program development approach breaks a
    problem down into its major sub_problems or
    components, each of which can then be dealt with
    independently of the others.

33
IN-CLASS PROBLEM SESSION 7 8
  • Objective
  • Learning programming with subroutines and
    functions
  • Assignment
  • Write a program to calculate the value of Poisson
    probability
  • function for ?3, N5 by using the function and
    subroutine
  • procedures.
  • Poisson probability function

34
Homework 7 8
  • Objective Learning programming with subroutines
    and functions Numerical examples (Numerical
    Solutions of Differential Equations - The Euler
    Method)
  • Assignment Write a program which uses Eulers
    method to obtain an approximate solution to a
    first order differential equation of y'2xy
    satisfying the initial condition y(0)1.
  • a) Write the program with subroutine.
  • b) Write the program with function.
  • c) Run the programs for x00.0 , y01.0,
    ?x0.05, N10.

35
  • Analytical solution

36
Numerical Solutions of Differential Equations
  • Equations that involve derivatives or
    differentials are called differential
  • equations. These equations arise in a large
    number of problems in
  • science and engineering. For many differential
    equations, it is very
  • difficult or even impossible to find the exact
    solution, but it may be
  • possible to find an approximate solution using a
    numerical method.
  • There are many methods. We describe the simplest
    one
  • Suppose we wish to approximate the solution of
    the first order
  • differential equation
  • y'f(x,y)
  • satisfying the initial condition
  • y(x0)y0
  • The Euler method for obtaining an approximate
    solution over some
  • interval (a,b) where ax0 is as follows (Nyhoff
    and Leestma, 1992)

37
(No Transcript)
38
Equation of line joining two points P1(x1,y1) and
P2(x2,y2)
  • Equation of line

39
Results of the analytical solution of y'2xy ()
and approximate numerical solution for x00.0 ,
y01.0, ?x0.05, N10 (solid line).
Write a Comment
User Comments (0)
About PowerShow.com