Functions and Subroutines - PowerPoint PPT Presentation

About This Presentation
Title:

Functions and Subroutines

Description:

A subroutine is a mini or sub program within your own program. ... The Function is place where a value of its ... There must be at least one RETURN statement ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 11
Provided by: DavidAG
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Functions and Subroutines


1
Functions and Subroutines
  • Covers Chapters 6 and 7. We will start with 7
    first.
  • A subroutine is a mini or sub program within your
    own program.
  • Subroutines are called on to perform a specific
    function
  • Why use subroutines?

2
Functions and Subroutines
  • Suppose you had to print a complex header on a
    page over and over again in several different
    places...how would you do it.
  • Suppose you had a complex arithmetic equation
    that you wanted to use in several different
    places on different variables.
  • Suppose a problem was so big you could not solve
    it in its whole.

3
Functions and Subroutines
  • You call a subroutine, when it is done, control
    returns to the statement after the call.
  • Example
  • XX1
  • Call SQUARE(X)
  • Print ,X

4
Format
  • Format of a call statement to a subroutine
  • CALL ROUTINENAME(ARGS)
  • Format of the subroutine itself
  • SUBROUTINE ROUTINENAME(ARGS)

5
Placement
  • Where do they go?
  • After the END statement in your program.

Main Program
Subroutines
6
Example
  • PROGRAM XYZ
  • INTEGER X
  • CALL SQUARE(X)
  • .
  • .
  • END
  • SUBROUTINE SQUARE(Y)
  • INTEGER Y
  • YYY
  • END

7
Functions
  • Functions are also sub programs but return a
    single value.
  • Functions are not called but placed in a regular
    Fortran Statement
  • The Function is place where a value of its same
    type is expected.
  • Functions cannot have values assigned to them
    only return values.

8
Functions
  • type FUNCTION name(arguments)
  • .
  • .
  • .
  • RETURN
  • END
  • Note Somewhere in the subroutine a value must be
    assigned to the name of the subroutine.

9
Example
  • REAL FUNCTION AVE(X,Y,Z)
  • READ X,Y,Z
  • AVE(XYZ)/3.0
  • RETURN
  • END
  • EXAMPLE OF HOW IT IS USED
  • AVETESTAVE(TEST1,TEST2,TEST3)

10
FUNCTIONS
  • There must be at least one RETURN statement
  • The type and word FUNCTION must proceed the name
    when writing the function.
  • A value matching the type of the function must be
    assigned to the name at least once in the
    function.
  • A return statement must exist in the function.
Write a Comment
User Comments (0)
About PowerShow.com