0% found this document useful (0 votes)
23 views

Lecture 4

1. This document discusses subprograms in Fortran, including functions and subroutines. 2. Functions perform simple tasks and return a single value, while subroutines can perform multiple tasks and return multiple values. 3. Benefits of subprograms include modularity, reusability, and protection against accidental changes to variables.

Uploaded by

Hassan GDOURA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lecture 4

1. This document discusses subprograms in Fortran, including functions and subroutines. 2. Functions perform simple tasks and return a single value, while subroutines can perform multiple tasks and return multiple values. 3. Benefits of subprograms include modularity, reusability, and protection against accidental changes to variables.

Uploaded by

Hassan GDOURA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

CENG 303

MATLAB, MAPLE, AND FORTRAN


FOR CHEMICAL ENGINEERS

MATTEO PASQUALI
[email protected]
713-348-5830
AL B-243

FORTRAN LECTURE 4
SUBPROGRAMS

PROBLEMS
1. PERFORM “SIMPLE” TASK MANY TIMES
AND RETURN A SINGLE VALUE,
POSSIBLY IN DIFFERENT PROGRAMS
FUNCTION

2. PERFORM “SIMPLE” TASK MANY TIMES


AND RETURN A SEVERAL VALUES,
POSSIBLY IN DIFFERENT PROGRAMS
SUBROUTINE
BENEFITS OF SUBPROGRAMS
1. SUBPROGRAMS ARE COMPILED SEPARATELY
AND CAN BE TESTED AND DEBUGGED
INDEPENDENTLY OF THE MAIN PROGRAM
AND OF EACH OTHER
2. SUBPROGRAMS CAN BE REUSED EASILY BY
OTHER PROGRAMS AND DO NOT NEED TO
BE DEBUGGED AGAIN
3. SUBPROGRAMS COMMUNICATE VARIABLES WITH
MAIN PROGRAM ONLY THROUGH AN
ARGUMENT LIST; VARIABLES NOT IN ARGUMENT
LIST CANNOT BE CHANGED ACCIDENTALLY AND
VARIABLES IN ARGUMENT LIST CAN BE
PROTECTED AGAINST ACCIDENTAL CHANGES
EXAMPLES OF FUNCTION TASKS

CHECK IF A NUMBER n IS PRIME


INPUT: n
OUTPUT: TRUE OR FALSE MANY
INPUTS
COMPUTE THE VALUE OF
A QUADRATIC EXPRESSION

y = ax + bx + c
2 FUNCTION

INPUT: a, b, c, x
OUTPUT: y ONE
BLACK BOX SEPARATED OUTPUT
FROM MAIN PROGRAM
FUNCTION STATEMENT
SYNTAX
function FUNCTION_NAME (ARGUMENT_LIST )
function statement is followed by
implicit none
declaration of all variables in argument list
declaration of type of FUNCTION_NAME
declaration of local variables used in function
statements used to calculate FUNCTION_NAME
end function FUNCTION_NAME

ANY VALID FORTRAN STATEMENT CAN BE


USED IN THE BODY OF A FUNCTION
EXAMPLE
COMPUTE THE VALUE OF A QUADRATIC EXPRESSION
y = ax + bx + c
2

INPUT: a, b, c, x
OUTPUT: y (value of quad. expr.) VALUE OF FUNCTION
IS STORED AND
function quadratic (x, a, b, c) RETURNED IN
FUNCTION NAME
implicit none
integer, parameter :: prec = selected_real_kind(13)
real (kind=prec), intent(in) :: x, a, b, c
real (kind=prec) :: quadratic
quadratic = a * x**2 + b * x + c
end function quadratic
EXAMPLE: VALUE OF A QUADRATIC EXPRESSION
program test
implicit none DECLARE
integer, parameter :: prec = selected_real_kind(13) FUNCTION
real (kind=prec) :: var, c1, c2, c3, yval NAME
real (kind=prec) :: quadratic
write(*,*) ‘coefficients of quadratic?’ NAME OF VARIABLES
read(*,*) c1, c2, c3 IN MAIN PROGRAM
write(*,*) ‘independent variable?’ AND FUNCTION
read(*,*) var
NEED NOT COINCIDE
yval = quadratic (var, c1, c2, c3)
write(*,*) ‘result’, yval TYPE OF VARIABLES
end program test IN MAIN PROGRAM
function quadratic (x, a, b, c)
AND FUNCTION
implicit none MUSTCOINCIDE
integer, parameter :: prec = selected_real_kind(13)
real (kind=prec), intent(in) :: x, a, b, c
real (kind=prec) :: quadratic
quadratic = a * x**2 + b * x + c
end function quadratic
SUBROUTINE STATEMENT
SYNTAX
subroutine SUB_NAME (ARGUMENT_LIST )
subroutine statement is followed by
implicit none
declaration of all variables in argument list
declaration of local variables used in subroutine
statements used to perform tasks
return
end subroutine SUB_NAME
ANY VALID FORTRAN STATEMENT CAN BE
USED IN THE BODY OF A SUBROUTINE
EXAMPLES OF SUBROUTINE TASKS

SORT A LIST OF n NUMBERS


INPUT: n, UNSORTED LIST
OUTPUT: SORTED LIST MANY
INPUTS
READ TWO ARRAYS X AND Y
OF SIZE n FROM A FILE
INPUT: filename, n, empty arrays
SUBROUTINE
OUTPUT: X, Y

MANY
BLACK BOX SEPARATED OUTPUTS
FROM MAIN PROGRAM
FUNCTIONS VS. SUBROUTINES
IN IN

FUNCTION SUBROUTINE

OUT OUT

• ONE OUTPUT • MANY OUTPUTS


• ONE TASK • SEVERAL TASKS
• RETURNS VALUE • RETURNS VALUE
THROUGH NAME THROUGH ARG. LIST
• REFERENCED THROUGH • REFERENCED WITH
NAME IN EXPRESSION CALL STATEMENT
y = funct(x) call sub(x,y)
SOMETIME EITHER STRUCTURE IS APPROPRIATE
EXAMPLE: SORTING A LIST OF NUMBERS
program test
implicit none
integer, parameter :: prec = selected_real_kind(13)
integer :: i NAME OF VARIABLES
real (kind=prec), dimension(10) :: list IN MAIN PROGRAM
write(*,*) ‘enter 10 numbers’
AND FUNCTION
read(*,*) (list(i), i = 1, 10)
call sort (10, list)
NEED NOT COINCIDE
write(*,*) ‘sorted list’, list TYPE OF VARIABLES
end program test IN MAIN PROGRAM
subroutine sort (n, values) AND FUNCTION
implicit none MUST COINCIDE
integer, parameter :: prec = selected_real_kind(13)
real (kind=prec), intent(in) :: n
real (kind=prec), intent(inout), dimension(n) :: values
integer :: k
...
return
end subroutine sort
PASSING VARIABLES THROUGH
ARGUMENT LIST

• NUMBER OF VARIABLES MUST MATCH


• ORDER OF VARIABLES MUST MATCH
• TYPE (REAL, INTEGER, ...) OF VARIABLES
MUST MATCH
• NAMES OF VARIABLES NEED NOT MATCH
PASSING VARIABLES THROUGH
...
ARGUMENT LIST
real :: a, b

SUBROUTINE
integer :: i, k

PROGRAM

“LABELS”
“LABELS”
logical :: test
...
call task (a, test, b, i, k)
...
subroutine task (x, check, y, m, n)
... MEMORY
integer :: m, n a x
real :: x, y b y
logical :: check i m
...
k n
return
end subroutine task test check
INPUT vs. OUTPUT VARIABLES
INTENT ATTRIBUTE
VARIABLES IN ARGUMENT LIST CAN BE:
• INPUT ONLY, CANNOT BE CHANGED IN SUBROUTINE
intent (in)
• INPUT-OUTPUT, MUST BE PREDEFINED AND
CAN BE CHANGED IN SUBROUTINE
intent (inout)
• OUTPUT ONLY, NO DATA SHOULD BE PASSED TO
SUBROUTINE THROUGH IT
intent (out)

ALWAYS SPECIFY INTENT


OF VARIABLES IN ARGUMENT LIST
EXAMPLE
...
real :: x, y, w, z
...
call sum_and_prod (x, y, w, z)
...
subroutine sum_and_prod (a, b, sum_ab2, prod_ab2)
! computes b^2, a+b^2, and a*b^2
implicit none
real, intent (in) :: a
real, intent (inout) :: b
real, intent (out) :: sum_ab2, prod_ab2
b = b**2
sum_ab2 = a + b
prod_ab2 = a * b
return
end subroutine sum_and_prod
REMEMBERING VARIABLES
SAVE ATTRIBUTE
LOCAL VARIABLES IN SUBROUTINES ARE
“FORGOTTEN” WHEN SUBROUTINE ENDS AND ARE
NOT USABLE IN SUBSEQUENT SUBROUTINE CALLS
UNLESS THEY ARE EXPLICITLY SAVED

...
real, save :: partial_sum
...

THE CURRENT VALUE OF partial_sum WILL BE


STORED WHEN SUBROUTINE ENDS AND
WILL BE RETRIEVE AT SUBSEQUENT CALL
PASSING ARRAYS TO SUBROUTINES
ALWAYS PASS ARRAY DIMENSIONS THROUGH
ARGUMENT LIST AND DIMENSION ARRAYS
EXPLICITLY IN DECLARATION OF VARIABLES
integer :: m, n
real, allocatable, dimension(:,:) :: A
...
read(*,*) m, n
allocate (A(m,n))
...
call test (m,n,A)
...
subroutine test (i,j,Amat)
implicit none
integer, intent(in) :: i, j
real, dimension(i,j), intent(inout) :: Amat
...
RETURN STATEMENT
EXITS A SUBROUTINE AND RETURNS TO PROGRAM
MUST BE PRESENT AT END OF SUBROUTINE

subroutine test (...)


...
do i = 1, n
...
if (...) then
return
endif
...
enddo
...
return
end subroutine test
OTHER FORTRAN 90 / 95 FEATURES
MODULE: program unit containing set of
definitions, data, and/or instructions

DERIVED DATA TYPE: variable containing


mixed integer / logical / real, etc. type

POINTER: variable containing memory address


of other variables

RECURSIVE PROCEDURE: function or


subroutine that can call itself

INTERNAL PROCEDURE: function or subroutine


that can be seen only by a certain program

You might also like