Procedures and Modular Programming Part # 2
Interface Block ► Functions do not have to be internal to main program ► They can be stand-alone ► In this case – the program that uses the function must have an INTERFACE block
Syntax interface type function name(arg-1,arg-2,…,arg-n) type, intent (in) :: arg-1 type, intent (in) :: arg type, intent (in) :: arg-3 end function name... other functions... end interface
Placing Interface Block ► After IMPLICIT NONE
Illustration ► Celsius and Fahrenheit Conversion ► Cm and Inch Conversion
Subroutines ► Program unit designed to perform a particular task ► Receives some input via formal arguments ► Returns the results (if any) with some formal arguments
Function vs. Subroutines ► Functions – designed to return a single value ► Subroutines – often return more than one value, even may return no value at all
Function vs. Subroutines Contd.. ► Functions – returns values via function name ► Subroutines – returns values via arguments
Function vs. Subroutines Contd.. ► Functions – referenced by using its name in an expression ► Subroutines – referenced by a CALL statement
Syntax – Form 1 subroutine subroutine_name (arg-1,…,arg-n) implicit none [specification part] [execution part] end subroutine subroutine_name
Syntax – Form 2 subroutine subroutine_name () implicit none [specification part] [execution part] end subroutine subroutine_name
Syntax – Form 3 subroutine subroutine_name implicit none [specification part] [execution part] end subroutine subroutine_name
Declaration of Arguments ► Argument that receives value from outside – declared with intent(in) ► Argument that does not receive value brut passes a computation to the outside – declared with intent(out ) ► Argument that does both – declared with intent(inout )
An example subroutine swap(a,b) implicit none integer, intent(inout) :: a,b integer :: temp temp=aa=bb=temp end subroutine swap
CALL Statement - Syntax CALL subroutine_name (arg-1,…,arg-n) or CALL subroutine_name() or CALL subroutine_name
Main Program program swapnum implicit none integer :: a,b a=5b=8print*,a*10+b call swap(a,b) print*,a*10+b end program swapnum
Module ► Parameters, variables, subprograms should be shared by separate programs ► Module – package of declarations and definitions; can be imported into other programs
Simplest Form module module-name implicit none [specification part] end module module-name
Illustration module circle implicit none real, parameter :: pi = real :: radius end module circle
Main Program program process_circle use circle implicit none print*,'radius?'read*,radiusprint*,area(radius)contains real function area(radius) implicit none real, intent(in) :: radius area=pi*radius**2 end function area end program process_circle
Module may also contain procedures module circle implicit none real, parameter :: pi = real :: radius contains real function area(radius) implicit none real, intent(in) :: radius area=pi*radius**2 end function area end module circle
New Main Program program process_circle use circle implicit none print*,'radius?'read*,radiusprint*,area(radius) end program process_circle