15 functions

26
Basic Scientific Programming Subprograms

Upload: fyjordan9

Post on 07-May-2015

148 views

Category:

Education


1 download

TRANSCRIPT

Page 1: 15 functions

Basic Scientific Programming

Subprograms

Page 2: 15 functions

Programming with functions

For complex problems it is helpful to divide the problem into a number of simpler subproblems.

Subprograms can be written to implement each of these subproblems.

In Fortran these subprograms are functions and subroutines whose execution is controlled by some other program unit. (main program or some other subprogram).

Page 3: 15 functions

Functions

Intrinsic functions Fortran provides many intrinsic (library)

functions.

These functions include numeric, character and logical functions.

Ex: Integer:: num1, num2, small … … small = min(0, num1,num2)

Page 4: 15 functions

Function Subprograms

In some programs it is convenient for the programmer to define additional functions. (Programmer-defined functions).

They are written as function subprograms, which are program units whose syntax is similar to that of a Fortran (main) program.

Function Subprogram Function Heading Specification Part

Execution Part End Function

Page 5: 15 functions

Function Heading

Function function_name(formal_argument_list)

or Type Function function_name(formal_argument_list)

function_name: may be any legal Fortran identifier

formal_argument_list: an identifier or a list of identifiers separated by commas.

Type: Optional type identifier (Integer, Real, Logical, or Character)

Page 6: 15 functions

Specification Part

Same form as the specification part of a Fortran program with the additional stipulations that: It must declare the type of the function value

if it has not been included in the function heading

It must declare the types of each of the functions’ formal arguments. These declarations should also contain an INTENT specifier that tells how the arguments are to transfer information.

Page 7: 15 functions

Execution Part

Same form as the execution part of a Fortran program with the additional stipulations that it should include at least one statement that assigns a value to the identifier that names the function. Function_name = expression

Expression involves constants, the formal arguments of the function, other variables already assigned values in the subprogram or references to other functions.

Page 8: 15 functions

The last statement must be:

END FUNCTION function_name

The value of the function will be returned to the program that referenced it when the END FUNCTION statement is encountered or when RETURN is executed

Page 9: 15 functions

Ex: Temperature Conversion

A subprogram to perform the conversion will have one formal argument representing a Fahrenheit temperature.

function Fahr_to_cel(temperature) Real:: Fahr_to_cel

Or Real function Fahr_to_cel(temperature)

Page 10: 15 functions

The formal argument “temperature” must be of type real and will be used ONLY to transfer information to the function.We need to declare temperature in the specification part.

Real, INTENT(IN):: temperature

Page 11: 15 functions

Function Fahr_to_Cel(temperature)Real:: Fahr_to_CelReal, INTENT(IN):: temperatureFahr_to_Cel=(temperature–32.0)/1.8End Function Fahr_to_Cel

Page 12: 15 functions

Types of Subprograms

Internal Subprogram:place it in the main program just before the END PROGRAM statement.

Module Subprogram:place it in a module from which it can be imported into the program.

External Subprogram: place it after the END PROGRAM statement of the main program.

Page 13: 15 functions

Internal Subprograms

Placed in the subprogram section at the end of the main program. The program unit that contains an internal subprogram is called a HOST for the subprogram.

form contains subprogram 1

subprogram 2 … subprogram n

Page 14: 15 functions

What is INTENT(IN)

Including the INTENT(IN) specification in a formal arguments’ declaration ensures that: The value of the corresponding actual

argument is passed to the formal argument

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

temperature= 0.0 “syntax error”

Page 15: 15 functions

Notes

If the INTENT(IN) clause is not used in the declaration of a formal argument, the value of the formal argument may be changed in the function and the value of the corresponding actual argument will also change.

The number and type of the actual arguments must agree with the number and type of the formal arguments.

Page 16: 15 functions

Ex: Function of Several Variables F(x,y,n) = xn + yn if x>=y

0.0 otherwise function F(x,y,n) real:: F real, INTENT(IN):: x,y integer, INTENT(IN):: n if(x>=y) then F = x**n + y**n else

F= 0.0 End If

End Function F !F(5,6,3) & F(6,5,3)

Page 17: 15 functions

Function airquality(poll_index)

Character(9):: airquality Integer,Intent(IN):: poll_index Integer,parameter:: cutoff=50

if (poll_index < cutoff) then airquality= “ safe” else airquality=“hazardous” end ifend function airquality

Page 18: 15 functions

Local Identifiers

Some functions require the use of constants and/or variables in addition to the formal arguments.

These constants and variables are called local identifiers.

The value of these identifiers is lost once execution of the subprogram is terminated.

Page 19: 15 functions

Ex Function Factorial(N) integer:: Factorial integer,INTENT(IN):: N integer:: I

Factorial = 1 Do I = 2,N,1 Factorial = Factorial * I End do End Function Factorial

Page 20: 15 functions

Poisson Probability Function

This is the probability function of a random variable.

λ = average number of occurrences per time period.n = number of occurrences in that time period.

Page 21: 15 functions

Scope

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 the subprogram. these items are said to be local.

Ex: I,N in the factorial example.

Page 22: 15 functions

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.

Changing the value of a global variable in one side of program will change the value of that variable throughout the entire program.

Page 23: 15 functions

Saving the values of local variables

The values of local variables in a subprogram are not retained from one execution of the subprogram to the next.

The save attribute allows holding the values.Ex: function f(..) integer, save:: count …… Count= count + 1 …… end function

Page 24: 15 functions

External Functions

The second type of subprogram is called external functions.

A subprogram can be made accessible to a program by attaching it after the END statement.

The function type should be declared in both the main program and the subprogram.

Page 25: 15 functions

Interfaces

Since the external subprogram is separate from the main program the compiler may not be able to check whether references to the subprogram are correct.

To ensure that the compiler can perform the necessary check, it is desirable that external functions have explicit interfaces.

Page 26: 15 functions

Interface

interface bodyEnd Interface

interface body consists of:1) The subprogram heading(except that different names may be used for the formal arguments).2) Declaration of the arguments and the result type.3) End function