Professional Documents
Culture Documents
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.
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.
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:
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.
(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
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
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)
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.
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 main program that called the SUBROUTINE FAC (F, X) above will look like this:
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
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
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
H=(Q-P)/FLOAT(N)
SUM=O
DO40I, =2, N
SUM=SUM+YC1)
4NCONTINUE
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?
If Statements
Functions
• 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/