(12) Functins Subroutines (Sina)

(12 Functins - Functions and Subroutines Sina Meraji Fall 2009 Review PROGRAM sumOfFactorials IMPLICIT NONE INTEGER n1,n2 result,I INTEGER Fact1

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

View Full Document Right Arrow Icon
Functions and Subroutines Sina Meraji Fall 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
Background image of page 4
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 5

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

View Full DocumentRight Arrow Icon
Deals with arguments that are variables The association is a bit more complex here… Rule 2
Background image of page 6
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 7

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

View Full DocumentRight Arrow Icon
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 = 1, b = 3, c = 3 WRITE (*,*) Minimum ( 18 , c - a , a + b ) My function Constant Expression Memory 13 27 18 37 3 10 a b c x y z
Background image of page 8
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 9

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

View Full DocumentRight Arrow Icon
Rule 2 : Variable 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 WRITE (*,*) Minimum ( a , b , c ) Memory 37 3 10 a b c x y z
Background image of page 10
Average of Three Numbers PROGRAM Avg IMPLICIT NONE REAL :: a, b, c, mean REAL :: Average READ (*,*) a, b, c mean = Average (a,b,c) WRITE(*,*) a, b, c, mean END PROGRAM Avg REAL FUNCTION Average (a,b,c) IMPLICIT NONE REAL :: a, b, c Average = (a+b+c) / 3.0 END FUNCTION Average
Background image of page 11

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

View Full DocumentRight Arrow Icon
Why we use subprograms 1 - Supports top down program design to simplify
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/15/2010 for the course COMP COMP 206 taught by Professor Vybihal during the Spring '04 term at McGill.

Page1 / 40

(12 Functins - Functions and Subroutines Sina Meraji Fall 2009 Review PROGRAM sumOfFactorials IMPLICIT NONE INTEGER n1,n2 result,I INTEGER Fact1

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

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