(11) Functions (Sina)

(11) Functions (Sina) - Functions Sina Meraji Fall 2009...

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

View Full Document Right Arrow Icon
Functions 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
What is a Function? Let’s look at an example
Background image of page 2
Example ± Get two numbers from the user ± Calculate the following expression C = A ! + B !
Background image of page 3

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

View Full DocumentRight Arrow Icon
! C = A! + B! PROGRAM sumOfFactorials IMPLICIT NONE INTEGER :: n1,n2, result,I INTEGER :: Fact1, Fact2 READ (*,*) n1 ! Input n1 Fact1 = 1 DO I = 1, n1 Fact1 = Fact1 * I END DO READ (*,*) n2 ! Input n2 Fact2 = 1 DO I = 1, n2 Fact2 = Fact2 * I END DO result = Fact1 + Fact2 ! C = A! + B! WRITE (*,*) “A! + B! = “,result END PROGRAM sumOfFactorials
Background image of page 4
! C = A! + B! PROGRAM sumOfFactorials IMPLICIT NONE INTEGER :: n1,n2, result,I INTEGER :: Fact1, Fact2 READ (*,*) n1 ! Input n1 Fact1 = 1 DO I = 1, n1 Fact1 = Fact1 * I END DO READ (*,*) n2 ! Input n2 Fact = 1 DO I = 1, n2 Fact2 = Fact2 * I END DO result = Fact1 + Fact2 ! C = A! + B! WRITE (*,*) “A! + B! = “,result END PROGRAM sumOfFactorials Do not look similar? Why bother repeating the same stuff. .? Why do not we write this section of codes separately and reuse them later on?
Background image of page 5

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

View Full DocumentRight Arrow Icon
! C = A! + B! PROGRAM sumOfFactorials IMPLICIT NONE INTEGER :: n1,n2, result,I INTEGER :: Fact1, Fact2, 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 Reduced code User defined function
Background image of page 6
Top Down Approach ± We begin by assuming we are able to determine the factorial of an given number. ± This is an example of Top-Down Programming Top-Down Programming means we assume that we have the solution to all sub-problems. We design solution based on that assumption, and at the end we solve the sub-problems. ± This high level solution makes an assumption that we already know how to find the factorial of a number
Background image of page 7

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

View Full DocumentRight Arrow Icon
PROGRAM sumOfFactorials IMPLICIT NONE INTEGER :: n1,n2, result,I INTEGER :: Fact1, Fact2, 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
Background image of page 8
Image of page 9
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 / 28

(11) Functions (Sina) - Functions Sina Meraji Fall 2009...

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

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