Structured Programming

Functions and Subroutines

Program Development

•     specify the program requirements

•     define the inputs and outputs

•     decompose the task into several smaller tasks

•     develop the overall structure of the program

•     develop a method for each basic task

•     write code for each task and test it

•     fit the code for each task into the overall program structure and test the entire code

Subprograms

•     Fortran 90 supports the structured program development process

•     main Fortran program may contain several subprograms

•     each subprogram performs a smaller task

•     subprograms may use other subprograms contained in the main program

•     two types of subprograms, functions and subroutines

Advantages of Subprograms

•     independent coding and testing of subtasks

–   input, output of subtask is clearly defined

–   variable names used in a subprogram are independent of those in other subprograms

•     reuse of code

–   once coded and tested, a subprogram may be used anywhere it is required

–   code maybe shared by many subprograms

–   easier to make modifications

 

Subprograms

•     subprograms have same structure as programs

•     subprograms communicate with the main program or other subprograms using arguments

•     arguments, also called parameters, are a list of variables specified in the subprogram definition

•     first statement of a subprogram defines the nature of the subprogram and its arguments

•     arguments must be declared in the subprogram and classified as input, output or both

Examples

function gcd(n,m)

! statements in the subprogram placed here

end function gcd

•     function subprogram with two arguments n,m

subroutine swap(x,y)

! statements in subroutine placed here

end subroutine swap

•     subroutine with two arguments x , y

 

Contains Statement

•     all subprograms used in the main program are placed after executable part of main program but before end program statement

•     contains statement used to indicate subprograms contained in main program

•     subprograms placed after contains statement

•     contains occurs at most once in main program

•     subprograms cannot contain other subprograms

 

Example

program example

! statements in program placed here

contains

function gcd(n,m)

! statements in function gcd

end function gcd

subroutine swap(x,y)

! statements in subroutine swap

end subroutine swap

end program example

Using Subprograms

•     (sub)program uses a subprogram by “calling” it

•     nature of call depends on type of subprogram

call subroutine_name(list_of_arguments)

function_name(list_of_arguments)

•     names given to subprograms are used

•     list_of_arguments must contain same number of variables as in the subprogram declaration

•     variable names maybe different but types and shapes of corresponding variables must match

Call Statement

•     transfers control to named subroutine

•     statements in subroutine are executed in order

•     control returns to statement following the call statement in the calling program on completion

•     corresponding variables in call statement and subprogram declaration are aliases

•     statement accessing an argument in the subprogram actually accesses corresponding variable in calling program

•     variables in calling program used in the call statement are called actual arguments

•     corresponding variables in the subprogram are called dummy arguments

•     dummy arguments are not assigned locations

•     share same location as corresponding actual argument (aliases)

•     values assigned to dummy arguments in subprogram get assigned to actual arguments

Function Subprograms

•     function subprograms called by just their name without an explicit call statement

•     values of actual arguments are not modified

•     function of the actual arguments is computed and its value returned in the function name

•     function name is a variable of required type in subprogram ( can also use result variable)

•     only one value “returned” to calling program

•     used in expressions like intrinsic functions

 

Example

program example

……

call swap(a,b)

a = gcd(a,b)

……

contains

function gcd(n,m)

……..

end function gcd

subroutine swap(x,y)

…..

end subroutine swap

end program example

Intent Attribute

•     specifies the way an argument to a subprogram is to be used

•     all arguments should have this attribute

•     intent(in) – input argument, value cannot be assigned to it by subprogram

•     intent(out) – output argument, must be assigned a value before use

•     intent(inout) – no restriction on use

•     function arguments are usually intent(in)

Local Variables

•     variables declared in subprogram other than  arguments are called local variables

•     local variables are assigned locations (“exist”) only when subprogram is called

•     local variables cannot be assigned values by any other subprogram or main program

•     variables declared in main program can be assigned values by subprograms contained in them unless same name is used for a local variable

–   should be avoided unless essential

 

Save Attribute

•     local variables normally “exist” only when a subprogram is executing

•     values lost when subprogram terminates

•     can be given save attribute

•     value from previous execution of subprogram reused in the next execution

•     count number of times subprogram is called

•     parameters and initialized variables in subprograms always have save attribute

Example (Subroutine)

subroutine swap(x,y)

integer, intent(inout) :: x,y

integer :: count =0                ! count has save attribute

integer  :: temp                     ! temp is not saved

temp = x ; x = y ; y = temp

count = count + 1

end subroutine swap

•      two arguments x,y of type input/output

•      temp is a local variable declared inside the subprogram

•      count is a local variable with save attribute

–   incremented every time subroutine is called

•      subroutine swaps the values of x and y

Example (Function)

function gcd(n,m)      result(g)

integer, intent(in) :: n,m

integer :: g, a, b, temp

a = n

b = m

do

    if ( b == 0) exit

    temp = b ; b = modulo(a,b) ; a = temp

end do

g = a

end function gcd

! result variable used to compute function rather than

! function name

Example (Using Subprograms)

program test

implicit none

integer :: i, j

read *,  i, j

if (i < j) call swap(i,j)       ! make i the larger number

print *,  i, j  

contains

subroutine swap(x,y)

…………

end subroutine swap

end program test

Example

•     find least common multiple (lcm) of several sets of numbers

•     lcm(n,m) = n*m/gcd(n,m)

•     lcm(n,m,l) = lcm(lcm(n,m),l)

•     use function gcd for computing lcm of two numbers

•     numbers read in sequence, terminated by 0

•     first number  0 indicates end of input

 

Example

program lcm_of_numbers

implicit none

integer :: n, lcm

new_sequence : do

print *, “enter first number of sequence”

print *, “enter 0 if no more sequences”

read *, n

if (n == 0) exit new_sequence ! end of input

lcm = n

new_number : do

print *, “enter next number in sequence”

print *, “enter 0 to terminate sequence”

read *, n

if (n == 0) exit new_number

lcm = lcm*n/gcd(lcm,n)

end do new_number

print *, “lcm is”, lcm

end do new_sequence

contains

function gcd(n,m)

………

end function gcd

end program lcm_of_numbers