11Functions-handout

11Functions-handout - Functions Amir Hossein Rabbani Winter...

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

Functions Amir Hossein Rabbani Winter 2009

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. What is a Function? Let’s look at an example
Example Get two numbers from the user Calculate the following expression C = A ! + B !

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

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

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

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

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

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

11Functions-handout - Functions Amir Hossein Rabbani Winter...

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

View Full Document
Ask a homework question - tutors are online