Subroutine Subprograms - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Subroutine Subprograms

Description:

All variables of data type character are 1 character in length unless otherwise specified ... of characters (i.e., words), must specify length in declaration ... – PowerPoint PPT presentation

Number of Views:80
Avg rating:3.0/5.0
Slides: 11
Provided by: beth74
Category:

less

Transcript and Presenter's Notes

Title: Subroutine Subprograms


1
Subroutine Subprograms
  • Program units designed to perform a particular
    task. They differ from functions by the
    following
  • Functions are designed to return a single value
    Subroutines often return more than one value or
    none at all
  • Functions return values via function names that
    must be declared Subroutines return values via
    arguments
  • Function is reference by using its name in an
    expression subroutine is referenced by a CALL
    statement

2
Subroutine Syntax
  • subroutine ltname of subprogramgt (ltargument
    listgt)
  • ltdeclare all variables usedgt
  • ltstatements of executiongt
  • return
  • end
  • Example
  • subroutine promptnum(tempC)
  • real tempC
  • print , Enter a temperature in Celsius
  • read , tempC
  • return
  • end

3
How to access a subroutine
  • call ltnamegt(ltargument listgt)
  • Example
  • call promptnum (tempC)
  • call printtable()
  • call doy2monday(doy,month,day,year)

4
Rules for the argument list
  • Variables being passed back and forth must be
    declared in both areas
  • Order of argument list must be same in the CALL
    line and the SUBROUTINE line
  • Argument list does not have to use the same
    variable names, but must reference same memory
    locations

5
Character data types
  • All variables of data type character are 1
    character in length unless otherwise specified
  • character gender
  • m or n if enter male, only m will be
    stored in the gender variable
  • To have variables be a string of characters
    (i.e., words), must specify length in declaration
  • character gender6, cyear4, fname32
  • character10 cmonth, cyear, cday

6
More about character strings
  • If character variable is allocated more units
    than string, string is left justified
  • character gender10
  • gender male
  • print , gender
  • Output male______

7
Combining and extracting strings
  • Concatenation in F77 //
  • character3 word1, word2
  • character words1and26, phrase20
  • word1 mon
  • word2 day
  • words1and2 word1 // word2
  • phrase Today is // word1 // word2
  • Extraction in F77
  • phrase Beth Hall is cool
  • word1 phrase(3) or word1 phrase(13)
  • word2 phrase(79)

8
We Try It!
  • Write a program that converts decimal degrees
    into degrees, minutes and seconds

9
We Try Another!
  • Write a program to convert polar coordinates (R,
    THETA) to rectangular coordinates (X, Y).

10
You Try It!
  • Write a program that has a starting unit table
    and a resulting unit table. In the starting unit
    table, provide an option to quit program. Based
    on the selection, either quit or display a table
    asking them to choose the conversion unit result
    (e.g., feet, kilometers, inches, meters, etc.).
    Based upon their choices, compute the result.
    Use subroutines, functions, IF statements, do
    whiles.
Write a Comment
User Comments (0)
About PowerShow.com