untitled

Similar documents
ForTran

C++ 程式設計

科学计算的语言-FORTRAN95

热设计网

2/80 2

untitled

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

錄...1 說...2 說 說...5 六 率 POST PAY PREPAY DEPOSIT 更

Microsoft Word - template.doc

移民資料

untitled

Microsoft PowerPoint - Lecture7II.ppt

untitled

untitled

cgn

untitled

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

K301Q-D VRT中英文说明书141009

第一章 簡介

厨房小知识(四)

妇女更年期保健.doc

小儿传染病防治(上)

<4D F736F F D B875B9B5A448ADFBBADEB27AA740B77EA4E2A5555FA95EAED6A641ADD75F2E646F63>

女性青春期保健(下).doc

避孕知识(下).doc

孕妇饮食调养(下).doc

禽畜饲料配制技术(一).doc

中老年保健必读(十一).doc

i

怎样使孩子更加聪明健康(七).doc

i

二零零六年一月二十三日會議

马太亨利完整圣经注释—雅歌

I/O Files讀寫檔案:

VHDL(Statements) (Sequential Statement) (Concurrent Statement) VHDL (Architecture)VHDL (PROCESS)(Sub-program) 2

untitled

untitled

untitled

untitled

untitled

untitled

東吳大學

Outline USB Application Requirements Variable Definition Communications Code for VB Code for Keil C Practice

untitled

untitled

目 錄 校 徽 圖 解 1 校 訓 釋 義 2 中 華 人 民 共 和 國 國 歌 3 順 德 聯 誼 總 會 屬 校 校 歌 4 辦 學 宗 旨 及 目 標 5 校 規 8 獎 懲 制 度 14 其 他 規 定 23 注 意 事 項 29 附 錄 33

天 主 教 輔 仁 大 學 社 會 學 系 學 士 論 文 小 別 勝 新 婚? 久 別 要 離 婚? 影 響 遠 距 家 庭 婚 姻 感 情 因 素 之 探 討 Separate marital relations are getting better or getting worse? -Exp

untitled

投影片 1

BC04 Module_antenna__ doc

97 CT試題補充(教師版).pdf

Microsoft Word - (web)_F.1_Notes_&_Application_Form(Chi)(non-SPCCPS)_16-17.doc

台灣經濟新報資料庫

untitled

婴幼儿护理(六).doc

逢 甲 大 學

Improved Preimage Attacks on AES-like Hash Functions: Applications to Whirlpool and Grøstl

地方公共服務績效比較評量之探討—標竿學習策略的觀點

OSWorkflow Documentation

GPS連續觀測網之地殼變動監測:資料品質管制系統之規劃

I

國科會專題研究計畫成果報告撰寫格式說明

untitled

untitled

行 1999 年 車 數 5526 數 42.64% 利 車 [3] 六年 年 料 車 例 例 38.5% 車 車 暴露 度 車 來 車 車 4] 不 若 車 車 車 行 車 度 車 度 車 行 料 車 行車 類 車 車 度 類 車 不 行車 理 略 車 2.1 類 車 行 理論 兩 理

untitled

Fuzzy GP

(Chi)_.indb

14A 0.1%5% 14A 14A

穨_2_.PDF

女性减肥健身(四).doc

Contents

年 參 類 來 識 見 錄 力 不 了 更 不 度 來說

PowerPoint 簡報

ARBURG Qualitätssicherung AQS 4.0

untitled

untitled

微處理機實習期末專題

ebook14-4

Contents

untitled

Microsoft Word - 發布版---規範_全文_.doc

概 述 随 着 中 国 高 等 教 育 数 量 扩 张 目 标 的 逐 步 实 现, 提 高 教 育 质 量 的 重 要 性 日 益 凸 显 发 布 高 校 毕 业 生 就 业 质 量 年 度 报 告, 是 高 等 学 校 建 立 健 全 就 业 状 况 反 馈 机 制 引 导 高 校 优 化 招

鱼类丰产养殖技术(二).doc

疾病诊治实务(一)

名人养生.doc

<4D F736F F D2040B9C5B871A661B0CFABC8AE61C2A7AB55ACE3A8735FA7F5ABD8BFB3B9C5B871A661B0CFABC8AE61C2A7AB55ACE3A8732E646F63>


中老年保健必读(十).doc

27 i

% % ,542 12,336 14,53 16,165 18,934 22,698 25, ,557 7,48 8,877 11, 13,732 17,283 22,

海淀区、房山区(四)

穨ecr1_c.PDF

穨2005_-c.PDF

北京理工大学.doc

尲㐵.⸮⸮⸮⸮⸮

东城区(下)

果树高产栽培技术(一).doc

物质结构_二_.doc

第一節 研究動機與目的

Transcription:

Fortran Chapter 7 Subroutine ( ) and Function 7-1 subroution 行 不 行 來 行 The general form of a subroutine is subroutine subroutine_name ( argument_list) (Declaration section) (Execution section) retrun end subroutine_name To call a subroutine, the calling program places a CALL statement in it s code. The form of a call statement is call subroutine_name ( argument_list) where the order and type of the actual arguments in the argument list must match the order and type of the dummy argunemts declared in the subroutine. Remark 立 數 若 了 數 不 不 1

Example Program ex0803 Implicit none integer :: A=1, b=2 call sub1() write(*,*) In main program: write(*, (2(A3,I3)) A=, A, B=, B stop end program ex0803 subroutine sub1() integer :: A=3, B=4 write(*,*) In subroutine sub1: write(*, (2(A3,I3)) A=, A, B=, B end 行 In subroutine sub1: A=3 b=4 In main program: A=1 b=2 Example Asimple subroutine to calculate the hypctenuse of a right triangle. subtoutine calc_hypotenuse( side_1, side_2, hypotenuse ) inplicit none real, intent(in) :: side_1, side_2 real, intent(out) :: hypotenuse real :: temp temp = side_1 ** 2 + side_2 ** 2 hypotenuse = sqrt(temp) end subroutine intent(in) 參數 不 數 intent(out) 參數 數 intent(inout) 參數 數 2

A test driver program fot subroutine calc_hyponteuse Program test_hypotenuse real :: S1, S2 real :: hypot write(*,*) Program to test suubroutine calc_hypotenuse: write(*,*) Enter the hength of side 1 read(*,*) S1 write(*,*) Enter the hength of side 2 read(*,*) S2 call calc_hypotenuse(s1, S2, hypot) write (*,10) hypot 10 Format(1X, The length of the hypotenuse is, F10.4) stop end program test_hypotenuse 3

7-1-1 variable passing in Fortran : The pass-by-reference scheme Fortran 參數 數 Program test real :: a, b(4) integer :: next call sub1(a, b, next) end program test subroutine sub1(x, y, i) real, intent(out) :: x real, dimension(4), intent(in) :: y integer :: i end subroutine Memory address Main program name Subroutine name 001 a x 002 b(1) y(1) 003 b(2) y(2) 004 b(3) y(3) 005 b(4) y(4) 006 nest i 007 Example Illustrating the effects of a type mismatch when calling a subroutine. Program bad_call Implicit none real :: x = 1.0 call bad_argument(x) end program bad_call subroutine bad_argument(i) integer ::i write(*,*) I=, i end subroutine 行 I=106535321.6 4

7-1-2 Passing arrays to subroutines There are two possible approaches to specify the length of a dummy array in a subroutine (1) pass the bounds of each dimension of the array to the subroutine as arguments in the subroutine call and to declare the corresponding dummy array to be that length. Example: Subroutine process1(data1, data2, n, nvals) integer, intent(in) :: n, nvals real, intent(in), dimension(n) :: data1 real, intent(out), dimension(n) :: data2 do i = 1, nvals data2(i) = 3.0 * data1(i) end do end subroutine process1 (2) Declare the length of each dummy array with an asterisk as an assumed-size dummy array. Example: Subroutine process2(data1, data2, nvals) real, intent(in), dimension(*) :: data1 real, intent(out), dimension(*) :: data2 integer, intent(in) :: nvals do i = 1, nvals data2(i) = 3.0 * data1(i) end do end subroutine process2 Not Good. Complier array size. 5

7-2 save The values of all local variables and arrays in a procedure become indefined when we exist the procedure. SAVE: guarantee the local variables and arrays to be saved unchanged between calls to a procedure. Example: Subroutine running_average(x, ave, nvals, reset) Implicit none real, intent(in) :: x real, intent(out) :: ave integer, intent(out) :: nvals logical, intent(in) :: reset! List of local variables: integer, save :: n real, save :: sum_x if (reset) then n = 0; sum_x = 0.0; ave = 0.0; nvals = 0 else n = n+1 sum_x = sum_x + x ave = sum_x / real(n) nvals = n end if end subroutine running_average 6

7-3 Sharing data rusing modules Example: Program main Implicit none type :: mytype type end type mytype stop end program main subroutine sub1() Implicit none type :: mytype end type mytype end subroutine sub1() type 7

subroutine mytype 料 module 來 module typedef Implicit none type :: mytype end type mytype end module typefef program main use type def stop end program main subroutine sub1() use type def end subroutine sub1 module 來 數 Example: Module vars real, save :: a, b, c end module vars 8

都 數 a, b, c Example: Module constants real, parameter :: pi=3.14159 real, parameter :: g=9.81 end module constants program main use constants stop end program main subroutine sub1() use constants end subroutine sub1 9

7-4 Fortran Functions Two different types of functions : intrinsic functions and User_defined functions Intrinsic functions are built into the Fortran language e.q. sin(x) and log(x) The general form of a user_defined Fortran function is Function name (argument_list) (Declaration section must declare type of name) (Execution section) 數 數 name = expr 數 數 end function [name] The type declaration of a user_defined Fortran function can take one of two equivalent forms: integer function my_function (i, j) or function my_function (i, j) integer :: my_function 10

Example: A function to evaluate a quadratic polynomial of the form real function quadf(x, a, b, c) real, intent(in) :: x, a, b, c quadf = a * x ** 2 + b * x + c end function 2 quad ( x) = ax + bx + c program test_quadf real :: quadf real :: a, b, c, x write(*,*) Enter quadratic coefficients a, b and c : read(*,*) a, b, c write(*,*) Enter location at which to evaluate equation : real(*,*) x write(*,100) quadf(, x, )=, quadf(x, a, b, c) 100 format(a, F10.4, A, F12.4) stop end program test_quadf The function should never modify its own imput arguments. 11

7-5 Passing user_definited functions as arguments. Example: program test real, external :: fun_1, fun_2 real :: x, y, output call evaluate(fun_1, x, y, output) call evaluate(fun_2, x, y, output) end program test subroutine evaluate(fun, a, b, result) real, external :: fun real, intent(in) :: a, b real, intent(out) :: result result = b * fun(a) end subroutine evaluate 12

7-6 Procedure interfaces and interface blocks Interface between the function/subroutine and a calling program unit The general form of an interface is interface interface_body_1 interface_body_2 end interface Each interface_body consists of the initial subroutine or function statement of the corresponding external procedure, the type specification statemts associated with its arguments, and an end subroutine or end function statement. 13

Example: Program ex0815 Implicit none real :: angle, speed interface function get_distance(angle, speed) real :: get_distance real, intent(in) :: angle, speed end function get_distance end interface write(*,*) Input shoot angle: read(*,*) angle write(*,*) Input shoot speed: read(*,*) speed: write(*, (T2, A4, F7.2, 1A) ) Fly, get_distance(angle, speed), m stop end program ex0815 function get_distance(angle, speed) real :: get_distance real, intent(in) :: speed, angle real :: rad real, parameter :: G=9.81 interface function angle_to_rad(angle) real :: angle_to_rad real, intent(in) :: angle end function angle_to_rad(angle) end interface rad = angle_to_rad(angle) get_distance = (speed * cos(rad)) * (2.0 * speed * sin(rad) / G) end function get_distance 14

function angle_to_rad(angle) real :: angle_to_rad real, intent(in) :: angle real, parameter :: pi=3.14159 angle_to_rad = angle * pi / 180.0 end function angle_to_rad Fortran 90 interface interface (i) 參數 來 參數 (ii) 參數數 不 (iii) 參數 (iv) 列參數 (v) 數 列 (vi) 數 15

7-7 不 數 參數 Fortran 90 optional 來 參數 略 Example Program ex0817 imteger :: a=10, b=>0 interface subroutine sub(a, b) integer, intent(in) :: a integer, intent(in), optional :: b end subroutine sub end interface write(*,*) Call sub with arg a call sub(a) write(*,*) Call sub with arg a, b call sub(a, b) stop end program ex0817 來 參數 不 subroutine sub(a, b) integer, intent(in) :: a integer, intent(in), optional :: b write(*,*) a if (present(b)) write(*,*) b 數 來 參數 end subroutine sub Output: Call sub with arg a 10 Call sub with arg a, b 10 20 數 present optional 參數 數 present 邏 參數.true..false. 類不 數 參數 interface 16

7-8 Recursive procedures 數 來 行 3! = 3 2! 2! = 2 1! 2! 1! 1! = 1 0! 0! := 1 0! Example: program ex0818 integer :: n, ans interface subroutine fact(n, and) function fact(n) result(ans) integer, intent(in) :: n integer, intent(inout) :: ans end subroutine fact end interface write(*,*) Input N: read(*,*) n call fact(n, ans) 省略 for function fact write(*, (t2, i2, a3, i10) ) n,!=, ans fact(n) stop end program ex0818 17

recursive subroutine fact(n, ans) integer, intent(in) :: n integer, intent(inout) :: ans integer :: temp if (n<0) then ans>0 end if if (n>=1) then call fact(n-1, temp) ans = n * temp else ans = 1 end if end subroutine fact 來 來 數來 來 數來 recursive function fact(n) result(ans) 數 數 來 數 integer, intent(in) :: n integer :: ans ans 數 數 select case(n) case(0) ans = 1 case(1) ans = n * fact(n-1) ans fact 來 數 case default ans = 0 end select end function fact 18

7-9 Contains statement ( ) Example: module module_example real :: x = 100.0 real :: y = 200.0 end module program scoping_test use module_example integer :: i = 1, j = 2 write(*, (A25, 2I7, 2f7.1) ) Beginning:, i, j, x, y call sub1(i, j) write(*, (A25, 2I7, 2f7.1) ) After sub1:, i, j, x, y call sub2(i, j) write(*, (A25, 2I7, 2f7.1) ) After sub2:, i, j, x, y contains subroutine sub2 real :: x x = 1000.0 y = 2000.0 Appears after the last excutable statement in program scoping_test. Only program scoping_test can use this subroutine sub2. write(*, (A25, 2F7.1) ) In sub2:, x, y end subroutine sub2 end program scoping_test 19

subroutine sub1(i, j) integer, intent(inout) :: i, j integer, dimension(5) :: array write(*, (A25, 2I7) ) In sub1 before sub2 :, i, j call sub2 write(*, (A25, 2I7) ) In sub1 after sub2 :, i, j array = (/(1000*i, i = 1, 5)/) write(*, (A25, 2I7) ) After array def in sub2 :, i, j, array contains subroutine sub2 integer :: i i = 1000 j = 2000 write(*, (A25, 2I7) ) In sub1 in sub2 :, i, j end subroutine sub2 end subroutine sub1 Module_example x, y Use Association Program scoping_test i, j local x, y from module i, j calling arguments Subroutine sub1 i, j dummy arguments local subroutine sub2 x local y inherited local subroutine sub2 i local j inherited 20

行 Beginning 1 2 100.0 200.0 In sub1 before sub2: 1 2 In sub1 in sub2: 1000 2000 In sub1 after sub2: 1 2000 After array def in sub2: 1 2000 1000 2000 3000 4000 5000 After sub1: 1 2000 100.0 200.0 In sub2: 1000.0 2000.0 After sub2: 1 2000 100.0 2000.0 module 數 module module_name use prher_module_name integer :: i type :: type_name end type :: type_name contains subroutine sub1(a) end subroutine sub1 function fun1(b) end function fun1 end module module_name 立 module module module module 數 數 module module 來 contains module 數 21

Example: module constants real, parameter :: pi = 3.14159 real, parameter :: g = 9.81 end module constants module calculate_distance use constants contains function argle_to_rad(angle) real :: angle_to_rad real, intent(in) :: angle angle_to_rad = angle * pi / 180.0 end function argle_to_rad function get_distance(speed, angle) real :: get_distance real, intent(in) :: speed, angle real :: rad rad = angle_to_rad(angle) get_distance = (speed * cos(rad)) * (2.0 * speed * sin(rad) / g) end function get_distance end module calculate_distance program ex0820 use calculate_distance write(*,*) Input shoot angle: read(*,*) angle write(*,*) Input shoot speed: read(*,*) speed write(*, (T2, A4, F7.2, 1A) ) Fly, get_distance(angle, speed), m stop end program ex0820 22

7-10 Intrinsic, external Datatype, external :: Func1, Func2 Func1 Func2 不 數 Intrinsic 來 real, intrinsic :: sin, cos 兩 省略 不 參數來 external intrinsic 不 省略 Example: program ex0821 real :: A = 30.0 real, intrinsic :: sin, cos real, external :: trig_func write(*,*) trig_func(sin, A) write(*,*) trig_func(cos, A) stop end program ex0821 function trig_func(func, x) real :: trig_func real, external :: func real, intent(in) :: x trig_func = func(x * 3.14159 / 180.0) end function trig_func 23