Procedures and Procedures and Modular Modular Programming Programming Part # 2 Part # 2
Procedures and Procedures and Modular Modular
ProgrammingProgramming
Part # 2Part # 2
Interface BlockInterface Block
►Functions do not have to be internal to Functions do not have to be internal to main programmain program
►They can be stand-aloneThey can be stand-alone
► In this case – the program that uses In this case – the program that uses the function must have an the function must have an INTERFACEINTERFACE blockblock
SyntaxSyntax
interfaceinterface
type function name(arg-1,arg-2,…,arg-n)type function name(arg-1,arg-2,…,arg-n)
type, intent (in) :: arg-1type, intent (in) :: arg-1
type, intent (in) :: arg-2type, intent (in) :: arg-2
. . . . . . . . . . . . . . . .
type, intent (in) :: arg-3type, intent (in) :: arg-3
end function nameend function name
. . . other functions . . .. . . other functions . . .
end interfaceend interface
Placing Interface BlockPlacing Interface Block
►After After IMPLICIT NONEIMPLICIT NONE
IllustrationIllustration
►Celsius and Fahrenheit ConversionCelsius and Fahrenheit Conversion
►Cm and Inch ConversionCm and Inch Conversion
SubroutinesSubroutines
►Program unit designed to perform a Program unit designed to perform a particular taskparticular task
►Receives some input via formal Receives some input via formal argumentsarguments
►Returns the results (if any) with some Returns the results (if any) with some formal argumentsformal arguments
Function vs. SubroutinesFunction vs. Subroutines
►Functions – designed to return a single Functions – designed to return a single valuevalue
►Subroutines – often return more than Subroutines – often return more than one value, even may return no value one value, even may return no value at allat all
Function vs. Subroutines Function vs. Subroutines Contd..Contd..
►Functions – returns values via function Functions – returns values via function namename
►Subroutines – returns values via Subroutines – returns values via argumentsarguments
Function vs. Subroutines Function vs. Subroutines Contd..Contd..
►Functions – referenced by using its Functions – referenced by using its name in an expressionname in an expression
►Subroutines – referenced by a Subroutines – referenced by a CALLCALL statementstatement
Syntax – Form 1Syntax – Form 1
subroutine subroutine_name (arg-1,…,arg-n)subroutine subroutine_name (arg-1,…,arg-n)
implicit noneimplicit none
[specification part][specification part]
[execution part][execution part]
end subroutine subroutine_nameend subroutine subroutine_name
Syntax – Form 2Syntax – Form 2
subroutine subroutine_name ()subroutine subroutine_name ()
implicit noneimplicit none
[specification part][specification part]
[execution part][execution part]
end subroutine subroutine_nameend subroutine subroutine_name
Syntax – Form 3Syntax – Form 3
subroutine subroutine_namesubroutine subroutine_name
implicit noneimplicit none
[specification part][specification part]
[execution part][execution part]
end subroutine subroutine_nameend subroutine subroutine_name
Declaration of ArgumentsDeclaration of Arguments
►Argument that receives value from Argument that receives value from outside – declared with intent(in)outside – declared with intent(in)
►Argument that does not receive value Argument that does not receive value brut passes a computation to the brut passes a computation to the outside – declared with intent(out )outside – declared with intent(out )
►Argument that does both – declared Argument that does both – declared with intent(inout )with intent(inout )
An exampleAn example
subroutine swap(a,b)subroutine swap(a,b)implicit noneimplicit noneinteger, intent(inout) :: a,binteger, intent(inout) :: a,binteger :: tempinteger :: temptemp=atemp=aa=ba=bb=tempb=tempend subroutine swapend subroutine swap
CALL Statement - SyntaxCALL Statement - Syntax
CALL subroutine_name (arg-1,…,arg-n)CALL subroutine_name (arg-1,…,arg-n)
oror
CALL subroutine_name()CALL subroutine_name()
oror
CALL subroutine_nameCALL subroutine_name
Main ProgramMain Program
program swapnumprogram swapnumimplicit noneimplicit noneinteger :: a,binteger :: a,ba=5a=5b=8b=8print*,a*10+bprint*,a*10+bcall swap(a,b)call swap(a,b)print*,a*10+bprint*,a*10+bend program swapnumend program swapnum
ModuleModule
►Parameters, variables, subprograms Parameters, variables, subprograms should be shared by separate should be shared by separate programsprograms
►Module – package of declarations and Module – package of declarations and definitions; can be imported into other definitions; can be imported into other programsprograms
Simplest FormSimplest Form
module module-namemodule module-name
implicit noneimplicit none
[specification part][specification part]
end module module-nameend module module-name
IllustrationIllustration
module circlemodule circle
implicit noneimplicit none
real, parameter :: pi = 3.101592real, parameter :: pi = 3.101592
real :: radiusreal :: radius
end module circleend module circle
Main ProgramMain Program
program process_circleprogram process_circleuse circleuse circleimplicit noneimplicit noneprint*,'radius?'print*,'radius?'read*,radiusread*,radiusprint*,area(radius)print*,area(radius)containscontains
real function area(radius)real function area(radius)implicit noneimplicit nonereal, intent(in) :: radiusreal, intent(in) :: radiusarea=pi*radius**2area=pi*radius**2end function areaend function area
end program process_circleend program process_circle
Module may also contain Module may also contain proceduresprocedures
module circlemodule circleimplicit noneimplicit nonereal, parameter :: pi = 3.141592real, parameter :: pi = 3.141592real :: radiusreal :: radiuscontainscontains
real function area(radius)real function area(radius)implicit noneimplicit nonereal, intent(in) :: radiusreal, intent(in) :: radiusarea=pi*radius**2area=pi*radius**2end function areaend function area
end module circleend module circle
New Main ProgramNew Main Program
program process_circleprogram process_circle
use circleuse circle
implicit noneimplicit none
print*,'radius?'print*,'radius?'
read*,radiusread*,radius
print*,area(radius)print*,area(radius)
end program process_circleend program process_circle