Presentation is loading. Please wait.

Presentation is loading. Please wait.

Procedures and Modular Programming

Similar presentations


Presentation on theme: "Procedures and Modular Programming"— Presentation transcript:

1 Procedures and Modular Programming

2 What are Procedures Program segments that have individual existence
Can be developed and tested separately Every Fortran Compiler comes with built-in procedures

3 Why Procedures Divide and conquer Private Library
Enable Fortran programmers to use programs developed by others

4 Types of Procedures Intrinsic functions Generic functions
Subroutine subprograms External functions

5 Functions type FUNCTION function-name (arg1, arg2, ….argn)
IMPLICIT NONE [specification part] [execution part] END FUNCTION function-name type FUNCTION function-name ()

6

7 Function Design Guidelines 1. Function Header
real function cylinder_vol (radius, height) implicit none

8 Function Design Guidelines 2. Declaring the Arguments
real function cylinder_vol (radius, height) implicit none real, intent (in) :: radius, height

9 Function Design Guidelines 3. Other Specifications
real function cylinder_vol (radius, height) implicit none real, intent (in) :: radius, height real, parameter :: pi=

10 Function Design Guidelines 4. Executable Statements
real function cylinder_vol (radius, height) implicit none real, intent (in) :: radius, height real, parameter :: pi= cylinder_vol = pi*radius**2*height

11 Function Design Guidelines 5. The End of a Function
real function cylinder_vol (radius, height) implicit none real, intent (in) :: radius, height real, parameter :: pi= cylinder_vol = pi*radius**2*height end function cylinder_vol

12 Function Design Guidelines 5. Function Name as a Special Variable
real function cylinder_vol (radius, height) implicit none real, intent (in) :: radius, height real, parameter :: pi= cylinder_vol = pi*radius**2*height end function cylinder_vol

13 Common Problems 1. Forget the type of a Function
function cylinder_vol (radius, height) implicit none real, intent (in) :: radius, height real, parameter :: pi= cylinder_vol = pi*radius**2*height end function cylinder_vol

14 Common Problems 2. Forget intent (in)
real function cylinder_vol (radius, height) implicit none real :: radius, height real, parameter :: pi= cylinder_vol = pi*radius**2*height end function cylinder_vol

15 Common Problems 3. Change the value of a formal argument
real function cylinder_vol (radius, height) implicit none real, intent (in) :: radius, height real, parameter :: pi= radius=3 cylinder_vol = pi*radius**2*height end function cylinder_vol

16 Common Problems 4. Forget to store a value to the function name
real function cylinder_vol (radius, height) implicit none real, intent (in) :: radius, height real, parameter :: pi= real :: volume volume = pi*radius**2*height end function cylinder_vol

17 Where Do Functions Go? Internal: part of the main program External

18 Internal program cy_vol implicit none ……… contains
real function cylinder_vol(radius,height) end function cylinder_vol end program cy_vol

19 How to use Functions? Use it as if you are using SIN(x), LOG(x)
The types of corresponding actual and formal arguments must be identical The number of actual and formal arguments must be equal


Download ppt "Procedures and Modular Programming"

Similar presentations


Ads by Google