next up previous
Next: Why Functions? Up: CS101 Lecture 10 Previous: Better Idea

Even Better Idea: Functions

Define a function getnum
Program Fndemo
 ! written on aug 23, 2000 by siva@cse.iitb.ernet.in 
 !  to demo use and usefulness of functions
Implicit none
Integer :: I, J, K  
 ! we want the user to type in 3 values.
 ! one between 1-100, one 501-600 and one 801-900
 ! functions is a nice way to avoid repetitive code and mistakes.
 I = Getnum(1, 100,   'Enter Temperature:  ')
 J = Getnum(501, 600, 'Enter Pressure:     ')
 K = Getnum(801, 900, 'Enter Volume:       ')
 Write (*,*) 'Temperature = ', I,  &
             ' Pressure = ', J,   &
             ' Volume = ', K

contains
   
  Integer Function Getnum(low, high, msg)
    Implicit none
    Integer, Intent(in) :: low, high
    Character(Len=20), Intent(in) :: msg
    Integer :: ans

    Do
       Write (*,*) msg, '(Between ', low, ' and ', high, ')'
       Read (*,*) ans
       If ((low <= ans) .and. (ans <= high)) Exit
       Write (*,*) 'You did not type a valid input. Please try again.'
    End Do
   Getnum = ans
  End Function Getnum

End Program Fndemo



G. Sivakumar 2000-08-23