12FunctinsSubroutines-handout

12FunctinsSubroutines-handout - Functions and Subroutines...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Functions and Subroutines Amir Hossein Rabbani Winter 2009
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Review PROGRAM sumOfFactorials IMPLICIT NONE INTEGER :: n1,n2, result,I INTEGER :: Fact1, Fact2 INTEGER :: Factorial READ (*,*) n1 ! Input n1 Fact1 = Factorial (n1) READ (*,*) n2 ! Input n2 Fact2 = Factorial (n2) result = Fact1 + Fact2 ! C = A! + B! WRITE (*,*) “A! + B! = “,result END PROGRAM sumOfFactorials Factorial Function
Background image of page 2
PROGRAM sumOfFactorials IMPLICIT NONE INTEGER :: n1,n2, result,I INTEGER :: Fact1, Fact2 INTEGER :: Factorial READ (*,*) n1 ! Input n1 Fact1 = Factorial (n1) READ (*,*) n2 ! Input n2 Fact2 = Factorial (n2) result = Fact1 + Fact2 ! C = A! + B! WRITE (*,*) “A! + B! = “,result END PROGRAM sumOfFactorials ! Computes the Factorial of n INTEGER FUNCTION Factorial ( n ) IMPLICIT NONE INTEGER :: n, value, i value = 1 DO i = 1,n value = value*i END DO Factorial = value END FUNCTION Factorial
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
How Are Arguments Initialized? It is not as simple as it might seem In general it varies from language to language There are 2 RULES that FORTRAN uses QuickTimeª and a decompressor are needed to see this picture.
Background image of page 4
QuickTimeª and a decompressor are needed to see this picture. The next few slides are very technical in nature… The material might be a bit tedious but it is important to understand It will become even more important later in the course when we study C
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
QuickTimeª and a decompressor are needed to see this picture. Rule 1 Tells us what happens when a constant or expression is used as an argument This is the most intuitive case
Background image of page 6
QuickTimeª and a decompressor are needed to see this pic Deals with arguments that are variables The association is a bit more complex here… Rule 2
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Rule 1 : Expression If an actual argument is an expression or a constant, it is evaluated and the result is saved into a temporary location. Then, the parameter becomes a reference to this temporary cell
Background image of page 8
Rule 1 : Expression When the function is invoked, new temporary variables, say X, Y and Z are created. The value of X is initialized to 18, Y to 27 and Z to 13. The function returns 13. INTEGER :: a = 10, b = 3, c = 37 WRITE (*,*) Minimum ( 18 , c - a , a + b ) My function Constant Expression Memory 10 3 37 18 27 13 a b c x y z
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Rule 2 : Variables If an actual argument is a variable, the corresponding formal argument is made to refer to the same memory cell.
Background image of page 10
When the function is invoked, there are no new variables created. The parameter x refers to a, y to b and z to c. We say x is an alias for a. There are two names for the same memory cell. The function returns 3. INTEGER :: a = 10, b = 3, c = 37
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 42

12FunctinsSubroutines-handout - Functions and Subroutines...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online