{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

12FunctinsSubroutines-handout

# 12FunctinsSubroutines-handout - Functions and Subroutines...

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

Functions and Subroutines Amir Hossein Rabbani Winter 2009

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

View Full Document
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
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

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

View Full Document
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.
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
Rule 2 : Variables If an actual argument is a variable, the corresponding formal argument is made to refer to the same memory cell.
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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 42

12FunctinsSubroutines-handout - Functions and Subroutines...

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

View Full Document
Ask a homework question - tutors are online