You are on page 1of 77

PET 516 LECTURE NOTE

Compiled by OMEH CHRISTIAN from: oylex.co


ALL RIGHT RESERVED
Modular programming
Modular programming is a term used to describe dividing a program into areas of codes that perform
specific tasks. The programmer designs the program in levels. The 1st level is the complete main program,
and modules at successive levels consists of submodules referenced in the prior level.

A module is a set of program statements which when acting together, complete a specific task and in most
cases can have one entry point and one exit point. In practice a module can be:
1. An arithmetic statement functions.
2. Subprogram.
3. Subroutine.

Modular programming offers the following advantages:


1. By dividing the solution of the problem into many parts, it’s possible to obtain a better insight into the
problem as a whole
2. It is quicker and easier to document specific tasks than to document an unstructured program.
3. Program maintenance like inserting new modules, amending codes and deleting codes from existing
modules is simpler.
Functions
There are two main types of functions. These are: Arithmetic statement functions and subprogram
functions.
Arithmetic statement function
This is the simplest type of module and is introduced into a program by a single defining statement. The
general form of an arithmetic function is:
Name (Arguments) = Arithmetic Expression.

E.g. RADIUS (x, y) = SQRT (X*X + Y*Y)

The arithmetic statement above, defines the function RADIUS. Since the name of the function begins with
an “R” it is regarded as a real function with two real arguments x and y. The implication of this is that x
and y can be used as variables elsewhere in the program.

Another example of a function statement is:

DISCRI (A, B, C) = B*B – 4.0*A*C

The above defines an arithmetic statement function called DISCRI. The name type shows it is a real
function with real arguments A, B and C. A close look at the statement shows that the function DISCRI
evaluates discriminant of a quadratic equation (AX2 + BX + C)
If a function has any variable on the right-hand side which does not feature in parenthesis with the name
on the LHS, such a variable must be defined elsewhere in the program. For example:

CVT (T) = T * CONST


Within the program, the statement
CONST = 4.546, must be defined elsewhere in the program previously.

SUBPROGRAM FUNCTIONS
Usually, a subprogram is part of a normal program written to perform some specific functions. It is
however independent of other parts of a program by the fact that it’s variables are local to it. This means
that a variable used in subprogram can also be used for some other things in other parts of the main
program. A function can be written as subprogram which may be referred to by a main program. Such a
program must start with a “FUNCTION” statement and end with an ‘END’ statement, it is usually of the
form:
FUNCTION Name (Arguments)

The name implicitly indicates a function type (integer or real) using the normal rules. In fortran,
declarations statements can be used to override the rules, the in-built function FLOAT can also be used to
convert an integer variable to real. The following are acceptable function statements in Fortran.

FUNCTION STATEMENT COMMENTS


(a) FUNTION AREA (A, B, C) Both function name and variables are real

(b) FUNCTION ITEM (X, Y, J, K) Function name is integer and variables are partly
real (x and y) and partly integer (J and K)
(c) INTEGER FUNCTION SAL (A, B, C) SAL has been declared an integer while variables
are real
(d) FUNCTION AREA (A, B, C) INTEGER A, B, C FUNCTION name is real and variables are declared
integers
A FUNCTION statement is followed by any required program that gives the value of the function. A
RETURN state is used to indicate completion of the function before the END statement.

Example 1

FUNCTION AREA (A, B, C)


S = 0.5 (A + B + C)
AREA = SQRT (S*(S-A) * (S-B) * (S-1))
RETURN
END
The subprogram calculates the area of the triangle

Example 2

Mathematically,
[N Combination R] = NCR
INTEGER FAC 1, FAC 2, FAC 3, FAC, R
[C READ THE VALUES OF N AND R]
READ (11, 30) N, R
30 FORMATS (215)
NO = N-R
[CALCULATE THE RESPECTIVE FACTORIALS OF N, R AND ND USING FUNCTION FAC]
FAC 1 = FAC (N)
FAC 2 = FAC (ND)
FAC 3 = FAC (R)
[C CALCULATE NCR]
NCR = FAC 1/ (FAC 2 * FAC 3)
[C PRINT OUT THE VALUES OF N, R, & NCR]
WRITE (12, 40) N, R, NCR
40 FORMATS (1X, ‘FOR N = ’15, ‘AND R=’ 15, ‘NCR=’ 15)
STOP
END
C THIS IS A FUNCTION SUBPROGRAM TO CALCULATE THE FACTORIAL OF X INTEGER FUNCTION FAC
(X)
INTEGER X

1. (Greater than or equal to)


LE (Less than or equal to)
C FIRST CHECK IF X IS ILLEGAL
IF (X. LT. 0) G TO 300
FAC = 1
C CHECK IF THE LOOP IS UNNECESSARY AND IF SO RETURN TO MAIN PROG
IF (X.LT.1) GO TO 200
IF (X.ET.0) GO TO 200
IF (X. ET.1) GO TO 200
C USE A LOOP TO CALCULATE CUMULATIVE PRODUCTS
DO 100 I = I, X
FAC = FAC*I
100 CONTINUE
200 RETURNS
300 WRITE (12, 50) X
50 FORMATS (1X, ‘FACTORIAL’ 15, ‘DOES NOT EXIST’)
FAC =0
RETURN
END

Assignment

Assignment (1)
If n is the number of trapezoids each of the width h, the total area of all the trapezoids is
A(n) = ½ h (y1 + 2y2 + 2y3 + …. + 2yn + yn+1)

Write a function subprogram for calculating the approximate area A(n)


Note: h = (e-p)/n
Assignment2
Write two arithmetic statement functions to compute

XR 1= (-b+√(b^2-4ac))/2a

XR2 = (-b-√(b^2-4ac))/2a

Subroutine Subprogram
A subroutine (in Fortran) is characterized by an initial SUBROUTINE statement which specifies the name
of the subroutine and a list of dummy arguments in brackets.

SUBROUTINE name (dummy arguments)

E.g. SUBROUTINE FAC (A, X)

In fortran, it must be noted that the name of the subroutine should not be more than six
alphanumeric characters. It’s spelling has no significance and to type (real or integer). The rule does not
apply to subroutine names. The variables however, are affected by the rule.
The body of a subroutine follows the ‘SUBROUTINE’ statement. The body can be used for input/output,
for manipulating arguments, or for calling other subroutines. The completion of an operation in a
subroutine is indicated by a return statement which returns control to the program or subprogram that
called it in the first instance.

The format of a subroutine is illustrated with subroutine the calculated factorials:

SUBROUTINE FAC (F, X)


INTEGER F, X
IF (X.LT. O) GO TO 300
F=1
IF (X.ET. O) GO TO 200
(Same body as the function subprogram)
END
SUBROUTINE CALL STATEMENTS: The services of a subroutine are invoked by the use of a call
statement. A general form of a call statement is:

CALL name (arguments)

The main program that called the SUBROUTINE FAC (F, X) above will look like this:

INTEGER FAC 1, FAC 2, FAC 3, R


READ (11, 30) N, R
30 FORMATS (215)
ND = N-R
C USE SUBROUTINE FAC TO CALCULATE THE RESPECTIVE FACTORIALS
C OF N, ND & R
CALL FAC (FAC 1, N)
CALL FAC (FAC 2, ND)
CALL FAC (FAC 3, R)
NCR = FAC 1/FAC 2*FAC 3)
WRITE (12, 40) N, R, NCR
40 FORMATS (1X, ‘FOR N=’ I5, ‘AND R=I5, ‘NCR=’15)
STOP
END

Example 2
SUBROUTINE TRIANG (AREA, A, B, C)
S = O.5*(A+B+C)
AREA = SQRT(S*(S-A) *(S-B) *(S-C))
RETURN
END

A CALL statement such as:


CALL TRIANG (X, 5 0, 4.0, 3.0) can call the above subroutine, where x is now set to the area of the triangle.
The subroutine differs from the function subprogram mainly by the fact that:
• They are used to compute more than one quantity while the function subprogram is used to
calculate only one quantity.
• Again, the function subprogram can be used directly as an operand in an arithmetic expression
and
• Function subprogram does not require a call statement as is the case with subroutines. (There are
the 3 basic differences between subroutines & f subgroups).

The common statement


The use of too many arguments/variables can sometimes be unacceptable to a programmer. Such a
situation can be avoided by declaring some area in the computer memory a common area which can be
shared by a main program and other subprograms. A common area is usually created in a main program
by the use of a statement. The general form:
COMMON VARIABLE LIST
For example:

DIMENSION FAM (10), NAP (5)


COMMON K, NAP, JAM

This will cause memory space to be set aside for variable k and arrays NAP and JAM in that order. This is
a total of 16 allocations.

K, NAP (1), NAP (2), NAP (3), NAP (4), NAP (5), JAM (1) … JAM (9), JAM (10). Any subsequent common
statement increases the space of the common area.

As long as a common area has been created by a main program, both function subprograms and
subroutines can make use of it provided they have their own common statements. The effect of a common
statement in a subprogram is to indicate where the variables which are common to both the main program
and the subprogram are to be found. For example;
It must be noted that a subroutine only shares common areas already declared by a main program.

Assignment2

Simpson’s rule:
Write a subroutine for the above expression

SUBROUTINE SIMPSN (AREA, Q, R, Y, N)

H=(Q-P) FLOAT N

SUM1=0

DO4I=2, N,2

SUM1=SUM1+Y(I)

4CONTINUE

SUM2=0

DO5I=3, N-1,2

SUM2=SUM2+F(I)

5CONTINUE

AREA=(H⁄3) *[Y(I)+4*SUM1+2*SUM2+Y(N+1)]

RETURN

END

SUBROUTINETRAPEZ (AREA, Q, P, YN)

H=(Q-P)/FLOAT(N)

SUM=O

DO40I, =2, N

SUM=SUM+YC1)

4NCONTINUE

AREA=(h⁄2) *(YC1) +2*SUM+Y(N+1)

RETURN

END
Program development
1. Understanding the problem: Understand what is required, the procedure, the processing, the inputs,
outputs etc.
2. Planning the solution: State and carefully study all mathematical equation and expression Depending
on the size of the problem, program may be shared between different programmer with each
programmes writing a separate module/segment of the program. Such modules may then be separately
tested before being brought together to be finally tested.
3. Drawing a flow-chart/flow diagram (pseudo codes). The flow chart will show the relationship
between part of the program. Time spent developing a flow chart pays off in reducing the time spent in
writing and correcting the program.
4. Writing the instructions (coding): From the flowchart, it shows clearly which sections of the program
is a subroutine, subprogram function etc. the codes are written in paper before punched unto a
computer.
5. Testing the program: After the codes have been punched unto a computer, it is then tested by running
it with a simple data sample which should give a result that is already known. This enable the
programmer to compare a known result a result from the program
6. Documentation: After the program has been tested and errors corrected (debugged) it is
comprehensively documented such a documentation should include statement of the problem, flow
charts, coding sheets, test procedures/results, changes/alterations made etc. if any part of the program
can be re-written and the task will be very much reduced if proper documentation exists from the
original program.
Math rule hasn’t changed since school

Tips !!!
Why did we get the wrong value when we ask the user to enter their bonus and salary values?

Here’s a hint-the input statement returns strings


CONTROL STRUCTURES

If Statements

You can use different symbols to check for different conditions


Write it the way you would say it

What do you think will happen if we type “YES” instead of “yes”


Is there a way we could change a string from upper case to lower case?

What if we try an IF statement with numbers instead of strings


Always test >, < and Boundary conditions

The “elif” allows you to check for different values


Functions

Functions

What if we could just create a button that does all that?


What is a Function

Why create functions?


How do you create functions?

How do you call a function?


To create a function that accepts data you use a parameter

A little note on the import statement


Your Challenge- Create a loan calculator.
• Attempts allowed No
• Duration4 days
• Passing grade5 point(s)
Overview:
Here, you will find attached the assignment, write in a print-read format and upload.

Access python online complier using the link:


http://www.compileonline.com/execute_python_online.php
• EXPRESSION
Link to Expression slides on oylex
https://oylex.co/courses/computer-application-in-petroleum-engineering/lessons/expressions/
• CONDITIONALS
Link to Conditionals slides on oylex
https://oylex.co/courses/computer-application-in-petroleum-engineering/lessons/conditional-
execution/

• FUNCTIONS
Link to Functions slides on oylex
https://oylex.co/courses/computer-application-in-petroleum-engineering/lessons/functions-3/
• LOOPS AND ITERATIONS
Link to Loops and Iterations slides on oylex
https://oylex.co/courses/computer-application-in-petroleum-engineering/lessons/iterations/

You might also like