fortran函数


A function subprogram is a subprogram that has a FUNCTION statement as its first statement.
R1215 function-subprogram   is 
函数子程序是这样的形式
function-stmt
[ specification-part ]
[ execution-part ]
[ internal-subprogram-part ]
end-function-stmt

R1216 function-stmt is [ prefix ] FUNCTION function-name ( [ dummy-arg-name-list ] ) [ RESULT ( result-name ) ]

Constraint: If RESULT is specified, the function-name must not appear in any specification statement in the
scoping unit of the function subprogram.
如果指定了RESULT关键字,那么函数名在函数子程序的范围中的任何语句中不能出现
R1217 prefix is type-spec [ RECURSIVE ]
or RECURSIVE [ type-spec ]
R1218 end-function-stmt is END [ FUNCTION [ function-name ] ]

Constraint: If RESULT is specified, result-name must not be the same as function-name.
如果指定了RESULT关键字,那么RESULT括号中的result_name不能与函数名相同
If both RECURSIVE and RESULT are specified, the interface of the function being defined is explicit within the  function subprogram.

The keyword RECURSIVE must be present if the function directly or indirectly invokes itself or a function
defined by an ENTRY statement in the same subprogram.

函数定义的例子
integer function gcd(a,b)
函数类型(表示函数返回值的类型),函数名,函数参数

fortran递归函数需在函数头添加recursive关键字。可以把函数执行结果返回给某个变量,比如下例中的answer
比如
recursive interger function gcd(i,j) result(answer)
用subroutine和function实现的gcd对比
module gcd_module
implicit none
contains
recursive subroutine gcd(i,j,res)
implicit none
integer,intent(in) ::i,j
integer,intent(inout) ::res
if(j==0)then
    res=i
else
    call gcd(j,mod(i,j),res)
endif
end subroutine gcd
end module
program main
use gcd_module
implicit none
integer ::i,j,result
print *,'type in two integer'
read*,i,j
call gcd(i,j,result)
print*,'gcd is ',result
end program main

module gcd_module
implicit none
contains
recursive integer function gcd(i,j) result(answer)
implicit none
integer,intent(in) ::i,j
if(j==0)then
    answer=i
else
    answer=gcd(j,mod(i,j))
endif
end function gcd
end module
program main
use gcd_module
implicit none
integer ::i,j,result
print *,'type in two integer'
read*,i,j
result=gcd(i,j)
print*,'gcd is ',result
end program main



program main
  2       implicit none
  3       integer,dimension(2,2) ::arr
  4       integer ::res
  5       integer ::i,j
  6       do i = 1,2
  7       do j = 1,2
  8         arr(i,j)=i*2+j
  9       end do
 10       end do
 11       call add(arr,res)
 12       write(*,*) res
 13 end
 14 
 15 subroutine add(arr,res)
 16       implicit none
 17       integer,dimension(2,2) ::arr
 18       integer ::i,j
 19       integer ::res
 20       res = 0
 21       do i = 1,2
 22       do j = 1,2
 23         res = res+arr(i,j)
 24       end do
 25       end do
 26 end

fortran可以支持数组的大小从子程序中传递,c++中则必须要用动态创建数组的方法
integer,dimension(s,s) ::arr
类似的这种c++定义则必须改由动态数组实现
  1 program main
  2       implicit none
  3       integer,dimension(2,2) ::arr
  4       integer ::res
  5       integer ::i,j
  6       do i = 1,2
  7       do j = 1,2
  8         arr(i,j)=i*2+j
  9       end do
 10       end do
 11       call add(arr,res,2)
 12       write(*,*) res
 13 end
 14 
 15 subroutine add(arr,res,s)
 16       implicit none
 17       integer ::s
 18       integer,dimension(s,s) ::arr
 19       integer ::i,j
 20       integer ::res
 21       res = 0
 22       do i = 1,s
 23       do j = 1,s
 24         res = res+arr(i,j)
 25       end do
 26       end do
 27 end


通过函数参数改变数组的值
 1 program main------
  2       integer ::s(5)
  3       call add(s)
  4       print*, 's(1)=',s(1),'s(2)=',s(2)  !输出s(1)=1  s(2)=4
  5 end

  1 subroutine add(s)
  2    implicit none
  3       integer ::s(5)
  4       integer ::i
  5       do i = 1,5
  6         s(i)= i*i
  7       end do
  8 end

References
Ian Chivers / Jane Sleightholme 
Introduction to Programming with Fortran- With Coverage of Fortran 90, 95, 2003, 2008 and 77      

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值