Upload
ignasi-miquel
View
219
Download
0
Embed Size (px)
Citation preview
8/12/2019 Fortran Subprogramming
1/15
ub programming
Dept. of ChemiCal engineering
8/12/2019 Fortran Subprogramming
2/15
Introduction Complex problems can be easily solved by using
divide-and-conquer strategy.
Problem is divided into a number of simpler sub-problems.
Each of these sub-problems are considered andsolved individually.
The complete solution is then a combination ofsolution to all these subproblems, i.e. subprogramswith the main program.
Dept. of ChemiCal engineering
8/12/2019 Fortran Subprogramming
3/15
Two types of subprogram in FORTRAN:
1. Function subprogram
2. Subroutine subprogram
Subprogram may also be I n te rna l or x te rna l . Usefulprocedures may be collected together as libraries. Suchcollections are called Modu l e s .
Dept. of ChemiCal engineering
ub programming
Main programs (i.e. everything we have seen so far),
external subprograms, and modules are referred to asp rog ram un i ts .
8/12/2019 Fortran Subprogramming
4/15
Internal Subprogram
Dept. of ChemiCal engineering
ub programmingPROGRAM progam_name
IMPLICIT NONE
[declaration statements]
[executable statements]CONTAINS
FUNCTION Name( [argument list] )
[declaration statements][executable statements]
END FUNCTION [Name]
END PROGRAM program_name
General
Syntax of
Internal
function
8/12/2019 Fortran Subprogramming
5/15
Function Subprogram
Dept. of ChemiCal engineering
ub programming
Function heading is a FUNCTION statement of the
form:
Name of the functionMay be any legal FORTRAN
identifier
List of identifiers separated bycommas used to pass
information to the function
subprogram
FUNCTION name (formal-argument-list)
Heading Part
8/12/2019 Fortran Subprogramming
6/15
Function Subprogram
Dept. of ChemiCal engineering
ub programmingSpecification Part
Specification part of a function subprogram has thesame form as the specification part of a FORTRANmain program.
It must include a specification of the type of thefunction.
8/12/2019 Fortran Subprogramming
7/15
Function Subprogram
Dept. of ChemiCal engineering
ub programmingExecution part
The execution part of a function subprogram
has the same form as the execution part of aFORTRAN program.
It must include at least one statement that
assigns a value to the identifier that names theexpression.
name = expression
8/12/2019 Fortran Subprogramming
8/15
Dept. of ChemiCal engineering
ub programming
PROGRAM Factorial
IMPLICIT NONE
INTEGER I
DO I = 1, 10PRINT*, I, Fact(I)
END DO
CONTAINS
FUNCTION Fact( N )
INTEGER Fact, N,J, Temp
Temp = 1
DO J = 2, N
Temp = J * Temp
END DO
Fact = Temp
END FUNCTION
END PROGRAM Factorial
Problem:Write a program that will show the 1 to 10 in firstcolumn and their factorials in 2nd column.
RUN
http://freeformat1.f95/http://c/Users/TOUFIQ/Desktop/FreeFormat2.EXEhttp://freeformat1.f95/http://c/Users/TOUFIQ/Desktop/FreeFormat2.EXE8/12/2019 Fortran Subprogramming
9/15
Dept. of ChemiCal engineering
ub programmingPROGRAM Newt on
! Sol ves f ( x) = 0 by Newt on' s methodI MPLI CI T NONEI NTEGER : : I t s = 0 ! i t er at i on count erI NTEGER : : MaxI t s = 20 ! maxi mum i t erat i onsLOGI CAL : : Conver ged = . f al se. ! conver gence f l agREAL : : Eps = 1e- 6 ! maxi mum err orREAL : : X = 2 ! st art i ng guessDO WHI LE ( . NOT. Converged . AND. I t s < MaxI t s)
X = X- F(X) / DF(X)PRI NT*, X, F(X)I t s = I t s + 1Conver ged = ABS( F( X) )
8/12/2019 Fortran Subprogramming
10/15
Dept. of ChemiCal engineering
ub programmingSummary of Internal function
Internal function are placed between a CONTAINS statement
and END statement of main program.
Internal function looks like the main program except the
header and end statement.
Internal function may not contain other subprogram.
Internal function has access of its host program. So the
variable declared in the host program is GLOBAL.
8/12/2019 Fortran Subprogramming
11/15
Dept. of ChemiCal engineering
ub programmingSubroutine
Subroutine subprograms differ from function subprograms
in the following respects:
Functions are designed to return one value.Subroutines often return more than one value, or
they may return no value at all.
Functions return values via function names;
subroutines return values via arguments.
A function is referenced by using its name in an
expression; subroutine is referenced by a CALL
statement
8/12/2019 Fortran Subprogramming
12/15
Dept. of ChemiCal engineering
ub programmingSubroutine Subprogram
Syntax of subroutine subprograms
Subroutine heading
Specification part
Execution part
8/12/2019 Fortran Subprogramming
13/15
Dept. of ChemiCal engineering
ub programmingSubroutine Subprogram
Subroutine heading is a SUBROUTINE statement of the
form:
SUBROUTINE name (argument list)
A subroutine is referenced by a CALL statement of theform:
CALL name (argument list)
8/12/2019 Fortran Subprogramming
14/15
Dept. of ChemiCal engineering
ub programming
PROGRAM SWAPPING
IMPLICIT NONE
REAL A, BREAD*, A, B
CALL SWOP( A, B )
PRINT*, A, B
CONTAINS
SUBROUTINE SWOP( X, Y )
REAL Temp, X, YTemp = X
X = Y
Y = Temp
END SUBROUTINE
END PROGRAM SWAPPING
Example: Write a program that will take two numbers from theuser and give the result after exchanging their position.
RUN
http://freeformat3.f95/http://freeformat3.f95/8/12/2019 Fortran Subprogramming
15/15
Dept. of ChemiCal engineering
ub programmingCLASS ASSIGNMENT
Write a computer program that reads appropriate
dimensions for different geometrical shapes (triangle,parallelogram, circle, trapezoid) and calculates their
areas.
Use sub programming (function and subroutine) to
solve the problem.