Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 8. Today’s program n New stuff: –Recursion in F –Procedures as arguments –User-defined data types n Quiz #3.

Similar presentations


Presentation on theme: "Week 8. Today’s program n New stuff: –Recursion in F –Procedures as arguments –User-defined data types n Quiz #3."— Presentation transcript:

1 Week 8

2 Today’s program n New stuff: –Recursion in F –Procedures as arguments –User-defined data types n Quiz #3

3 What is recursion? n Recursion in programming is a technique for defining a problem in terms of one or more smaller versions of the same problem. n The solution to the problem is built on the result(s) from the smaller version(s).

4 What is recursion? n Best known example: the factorial n! = n (n-1)! 0! = 1 n That’s all you need to calculate n!

5 Recursive procedures n recursive function function_name(…) result(result) n Let’s use it to calculate n! n Structure plan n = 0 factorial_n = 1 n > 0 factorial_n = n * factorial(n-1) n < 0 Error! Return factorial_n = 0

6 Solution 1 n recursive function factorial(n) result(factorial_n) ! Dummy argument and result variable integer, intent(in) :: n real :: factorial_n ! Determine whether further recursion is required select case(n) case(0) ! Recursion has reached the end factorial_n = 1.0 case(1:) ! More recursive calculation(s) required factorial_n = n*factorial(n-1) case default ! n is negative - return zero as an error indicator factorial_n = 0.0 end function factorial

7 Solution 2 n recursive subroutine factorial(n, factorial_n) ! Dummy arguments integer, intent(in) :: n real, intent(out) :: factorial_n ! Determine whether further recursion is required select case(n) case(0) ! Recursion has reached the end factorial_n = 1.0 case(1:) ! Recursive call(s) required ! to obtain (n-1)! call factorial(n-1, factorial_n) case default ! n is negative - return zero as an error indicator factorial_n = 0.0 end subroutine factorial

8 Procedures as arguments Up to now, procedures had as dummy arguments real, integer, character, logical and arrays with these types n But, what about a procedure as a dummy argument?

9 How to declare a procedure? n We have to provide information concerning the interface of the procedure The interface block: interface interface_body end interface

10 How to declare a procedure? Example: interface function dummy_fun(a, b) result(r) real, intent(in) :: a, b real :: r end function dummy_fun end interface

11 How to declare a procedure? Example: interface subroutine one_arg(x) real, intent(inout) :: x end subroutine one_arg recursive subroutine two_args(x, y) real, intent(inout) :: x, y end subroutine two_args end interface

12 Couple of details... n Dummy procedure arguments in a function must be functions n All actual procedure arguments must be module procedures n It is not permissible for an intrinsic procedure to be an actual argument

13 Derived data types n You can create your own data types! n They can be derived from: –Intrinsic data types: integer, real, character, logical –Previously defined data types

14 Derived data types type, public :: new_type component_definition... end type new_type

15 Example type, public :: person character(len=12) :: first_name character(len=1) :: middle_initial character(len=12) :: last_name integer :: age character(len=1) :: sex ! M or F character(len=5) :: tax_number end type person

16 Example Now you can declare variable of type person : type(person) :: ali, mukaddes, veli

17 Derived types vs. arrays 123456 A

18 Be careful... n Declarations need access to the type definition n Place definitions in a module

19 Putting data in a derived type variable ali = person("Ali","M","Aktepe",56,"M","45645") mukaddes = person("Mukaddes"," ","Has",18,"F","12345") veli = person("Veli","M","Karatepe",65,"M","34567") This is a structure constructor

20 Refering to components n variable%component Example: ali%last_name mukaddes%age veli%first_name

21 Using derived types to define new derived types n type, public :: employee type(person) :: employee character(len=20) department real :: salary end type employee n type(person) :: saadet n saadet%employee%age = 34

22 Arrays of derived types n type(person), dimension(1:12) :: bil102class Then: bil102class(2)%sex = "M" You can do operations on components : bil102class(3)%age+bil102class(4)%age But no such operation is permissible: bil102class(3) - bil102class(4)

23 Example: Let's do some geometry! n Two derived types: –point –line n Two functions: –distinct_points(p1,p2) result(distinct) –line_from_points(p1,p2) result(join_line)

24 Assignment due on April 22 n Problem 8.3 from Ellis & Philips n Make sure to fully document your programs!


Download ppt "Week 8. Today’s program n New stuff: –Recursion in F –Procedures as arguments –User-defined data types n Quiz #3."

Similar presentations


Ads by Google