27 march, 2000 cs1001 lecture 16 functions subroutines scope modules external subprograms

23
27 March, 2000 CS1001 Lecture 16 • FUNCTIONS • SUBROUTINES • SCOPE • MODULES EXTERNAL SUBPROGRAMS

Post on 21-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

27 March, 2000

CS1001 Lecture 16

• FUNCTIONS• SUBROUTINES• SCOPE• MODULES• EXTERNAL SUBPROGRAMS

27 March, 2000

Function General FormFUNCTION function_name (formal_argument_list)

type_identifier :: function-name

(Declaration section)

(Execution section)

function_name = expr

END FUNCTION function_name

formal_argument_list is a list of identifiers (may be blank)

type_identify is used to identify the type of the FUNCTION

Alternate Heading:

type_identifier FUNCTION function_name (formal_argument_list)

27 March, 2000

Argument Passing

• Arguments are used to pass information between (to/from) caller and callee.

• INTENT Specifier Tells how the arguments are to transfer information– type, INTENT(IN) :: argument for inputs TO either a

function or subroutine– type, INTENT(OUT) :: argument for outputs FROM a

function or subroutine (but not good practice to have OUT arguments in function)

– type, INTENT(INOUT) :: argument for both TO and FROM a subprogram

27 March, 2000

INTENT (IN) specification

• Used to ensure– value of the actual argument is passed to the formal

parameter

– the value of the formal argument cannot be changed while the function is being executed

• Well designed Fortran function– produce a single output value from one or more input

values

– never modify its own input arguments -- always declare the arguments with INTENT(IN) attribute.

27 March, 2000

Unintended side effects

• Changing the value of a variable in one part of the program affects, unintentionally, the value of that variable or other variables in other parts of the program.

• Often dangerous because – they might cause wrong results

– are hard to debug (therefore, wrong results might go unnoticed.)

27 March, 2000

Old FORTRAN IV Example

In calling programSum = 0Sum = addsum (10). . .Sum = Sum + 10

Print *, Sum

In Function addsum (IMAX). . .IMAX = 5addsum = . . . (say 20)END

Name value in storageSum 010 10IMAX 10, then 5

1

2

4

3

1

Name value in storageSum 2010 5

35

4

Name value in storageSum 2010 5

56

Name value in storageSum 2510 5

After 5

At 6 Print Sum will give 25

2

27 March, 2000

SUBROUTINE General Form

SUBROUTINE Subroutine_name (formal_argument_list)

(Declaration section)

(Execution section)

subroutine_name = expr

END SUBROUTINE subroutine_name

formal_argument_list is a list of identifiers (may be blank)

27 March, 2000

Subroutine Reference

• Each subroutine should have a header. • The program using the subroutine does not declare

the subroutine.• The subroutine is referenced via a CALL

statement• The code for the subroutine follows the main

program CONTAINS statement and precedes the END PROGRAM statement

27 March, 2000

Subroutine Example

SUBROUTINE DDMMSS(Angle)

REAL, INTENT(IN) :: Angle

INTEGER :: Deg, Min, Sec

Deg = INT(Angle)

Min = INT(Angle * 60) - (Deg * 60)

Sec = INT(Angle * 3600) - (Deg * &3600) - (Min * 60)

PRINT *, Angle, ‘ degrees = ‘

PRINT *, Deg, ‘ deg ‘, Min, ‘ min& & ‘, Sec, ‘ sec’

END SUBROUTINE DDMMSS

27 March, 2000

Subroutine Calling

• Within a program, the subroutine is called as:CALL DDMMSS(Input)

• At this point in the program execution, the subroutine DDMMSS is called with angle Input as an argument, causing the subroutine to execute and display the angle in DD MM SS format

27 March, 2000

Subroutine Argument Return

SUBROUTINE DDMMSS(Angle, DDD, MM, SS)

REAL, INTENT(IN) :: Angle

INTEGER, INTENT(OUT) :: DDD, MM, SS

DDD = INT(Angle)

MM = INT(Angle * 60) - (DDD * 60)

SS = INT(Angle * 3600) - (DDD * & 3600) - (MM * 60)

END SUBROUTINE DDMMSS

27 March, 2000

Subroutine Argument Return

• The program would include the following statements:CALL DDMMSS(Input, Deg, Min, Sec)

PRINT *, Input, ‘ degrees = ‘

PRINT *, Deg, ‘ deg ‘, Min, ‘ min ‘, Sec, ‘ sec‘

• Variables DEG, MIN, and SEC are passed to the subroutine to store the computed values for those variables

Input AngleDeg DDDMin MMSec SS

Actual arguments

Formal arguments

27 March, 2000

Converting With SubroutineDO iRange = Init, Limit, iStep

Range = iRange

CALL CTOF(Range, Fahr)

PRINT *, Range, Fahr

END DO

Where:

SUBROUTINE CTOF(Cel, Far)

REAL, INTENT(IN) :: Cel

REAL, INTENT(OUT) :: Far

Far = 1.8 * Cel + 32.0

END SUBROUTINE CTOF

Fahr = CTOF(Range)

FUNCTION CTOF(Cels)REAL :: CTOFREAL, INTENT(IN) :: CelsCTOF = 1.8 * Cels + 32.0END FUNCTION CTOF

27 March, 2000

Scope

• The portion of the program in which an entity (variable, constant, subprogram, types) is visible, i.e., where it is accessible and can be used.

• Fundamental Principle -- The scope of an entity is the program or subprogram in which it is declared

• Scope Rule 1 -- An item declared within a subprogram is not accessible outside that subprogram

• Scope Rule 2 -- A global entity is accessible throughout the main program and in any internal subprogram in which no local entity has the same name as the global item.

27 March, 2000

Scope examplesPROGRAM MAININTEGER :: X, Y::

CONTAINSSUBROUTINE SUB1(…)REAL :: Y:END SUBROUTINE SUB1SUBROUTINE SUB2(…)INTEGER:: X:END SUBROUTINE SUB2END PROGRAM MAIN

Y of SUB1

X of MAIN

X of SUB2

Y of MAIN

Y of MAIN

27 March, 2000

SAVE attribute

• Values of local variables in a subprogram are not retained from one execution to the next

• A variable in a subprogram can retain value if– initialized in the declaration– have SAVE attribute type, SAVE :: list_of_local_variables if

list_of_local_variables is omitted, all local variables are saved– For example:INTEGER FUNCTION ADD(…)

INTEGER :: COUNT = 0, SUM=0

INTEGER, SAVE :: RUNING_TOTAL

:

END FUNCTION ADD

27 March, 2000

MODULES• A Module

– is a program unit to package together type declarations, subprograms, and definitions of new data types

– provides a way to bundle a library of useful code to be used in other program units

• Form:MODULE module_name

CONTAINSsubprogram1

subprogram2

:

type_declarations

END MODULE module_nameE.g.,REAL, PARAMETER:: PI=3.14159

27 March, 2000

Modules

• Modules, once defined, can be used in a programming unit by a USE statement

• For Example,PROGRAM Complex_Calc

USE complex_functions

:

END PROGRAM Complex_Calc

27 March, 2000

Preparing a Program for Execution

You enter the program and save it as a source file

The compiler attempts to translate the program

You correct syntax error

The linker links the new object file with other object files

The loader places the load file into memory

Executable program in memory

Load File

Source fileon disk

New Object

File

OtherObject

File

Revised Source file

List of errors

Oops!

Good job!

27 March, 2000

Linking Programs and Modules

Program source file

Module source file

Program Object file

Module Object file

F90 Compiler

F90 Compiler

LinkerExecutable File

27 March, 2000

External Subprograms

• Subprogram attached after the END PROGRAM statement

• Separate and independent from main program• No access to each others local variables• Information sharing through function name and

arguments• Use Interface blocks to ease compatibility checking

INTERFACE

Interface_body

END INTERFACE

27 March, 2000

Internal vs External Subprograms -Forms

Internal:PROGRAM Maintype :: function_name:expr = function_name (..)call subroutine_name (..):INCLUDEStype FUNCTION function_name (..):function_name = expr1END FUNCTION function_name SUBROUTINE subroutine_name:END SUBROUTINE subroutine_nameEND PROGRAM Main

External:PROGRAM MainINTERFACE FUNCTION function_name (..) type :: function_name type :: arguments_list END FUNCTION function_name END INTERFACE:expr = function_name (..):END PROGRAM MainFUNCTION function_name (..) type :: function_name type :: arguments_list:END FUNCTION function_name

27 March, 2000

Module - Form

ModuleMODULE module_name

INCLUDEStype FUNCTION function_name (..):function_name = expr1END FUNCTION function_name SUBROUTINE subroutine_name (..):END SUBROUTINE subroutine_name:END MODULE module_name

PROGRAM Main:USE MODULE module_name:expr = function_name (..)CALL subroutine_name (..):END PROGRAM Main