Top Banner
Procedures and Procedures and Modular Modular Programming Programming Part # 2 Part # 2
22

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.

Jan 05, 2016

Download

Documents

Carmel Horn
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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.

Procedures and Procedures and Modular Modular

ProgrammingProgramming

Part # 2Part # 2

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

Placing Interface BlockPlacing Interface Block

►After After IMPLICIT NONEIMPLICIT NONE

Page 5: 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.

IllustrationIllustration

►Celsius and Fahrenheit ConversionCelsius and Fahrenheit Conversion

►Cm and Inch ConversionCm and Inch Conversion

Page 6: 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.

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

Page 7: 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.

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

Page 8: 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.

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

Page 9: 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.

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

Page 10: 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.

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

Page 11: 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.

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

Page 12: 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.

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

Page 13: 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.

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 )

Page 14: 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.

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

Page 15: 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.

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

Page 16: 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.

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

Page 17: 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.

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

Page 18: 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.

Simplest FormSimplest Form

module module-namemodule module-name

implicit noneimplicit none

[specification part][specification part]

end module module-nameend module module-name

Page 19: 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.

IllustrationIllustration

module circlemodule circle

implicit noneimplicit none

real, parameter :: pi = 3.101592real, parameter :: pi = 3.101592

real :: radiusreal :: radius

end module circleend module circle

Page 20: 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.

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

Page 21: 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.

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

Page 22: 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.

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