Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Procedures and Modular Programming Part # 2

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

3 Syntax interface type function name(arg-1,arg-2,…,arg-n) type, intent (in) :: arg-1 type, intent (in) :: arg-2................ type, intent (in) :: arg-3 end function name... other functions... end interface

4 Placing Interface Block ► After IMPLICIT NONE

5 Illustration ► Celsius and Fahrenheit Conversion ► Cm and Inch Conversion

6 Subroutines ► Program unit designed to perform a particular task ► Receives some input via formal arguments ► Returns the results (if any) with some formal arguments

7 Function vs. Subroutines ► Functions – designed to return a single value ► Subroutines – often return more than one value, even may return no value at all

8 Function vs. Subroutines Contd.. ► Functions – returns values via function name ► Subroutines – returns values via arguments

9 Function vs. Subroutines Contd.. ► Functions – referenced by using its name in an expression ► Subroutines – referenced by a CALL statement

10 Syntax – Form 1 subroutine subroutine_name (arg-1,…,arg-n) implicit none [specification part] [execution part] end subroutine subroutine_name

11 Syntax – Form 2 subroutine subroutine_name () implicit none [specification part] [execution part] end subroutine subroutine_name

12 Syntax – Form 3 subroutine subroutine_name implicit none [specification part] [execution part] end subroutine subroutine_name

13 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 )

14 An example subroutine swap(a,b) implicit none integer, intent(inout) :: a,b integer :: temp temp=aa=bb=temp end subroutine swap

15 CALL Statement - Syntax CALL subroutine_name (arg-1,…,arg-n) or CALL subroutine_name() or CALL subroutine_name

16 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

17 Module ► Parameters, variables, subprograms should be shared by separate programs ► Module – package of declarations and definitions; can be imported into other programs

18 Simplest Form module module-name implicit none [specification part] end module module-name

19 Illustration module circle implicit none real, parameter :: pi = 3.101592 real :: radius end module circle

20 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

21 Module may also contain procedures module circle implicit none real, parameter :: pi = 3.141592 real :: radius contains real function area(radius) implicit none real, intent(in) :: radius area=pi*radius**2 end function area end module circle

22 New Main Program program process_circle use circle implicit none print*,'radius?'read*,radiusprint*,area(radius) end program process_circle


Download ppt "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."

Similar presentations


Ads by Google