Fortran 中的函数与子程序

Fortran中的函数用于有输入和输出的计算,通过FUNCTION定义,如calculate_area示例。子程序则不返回值,通过SUBROUTINE定义,如print_message和calculate_sum。它们允许程序执行特定任务并可在代码中多次调用。函数适合返回结果,子程序适合执行操作或修改参数。
摘要由CSDN通过智能技术生成

Fortran 中的函数与子程序

简介

  • Fortran 是不区分大小写的
  • 函数(Function):
    • 函数是一段具有输入和输出的代码块,它接受一些输入参数,经过一系列计算后返回一个结果。
      • 在Fortran中,函数的定义以关键字"FUNCTION"开始,以"end function"结束
      • Fortran 90中的函数必须在程序的`contains`块中定义,以便在主程序中调用
  • 子程序(Subroutine):
    • 子程序是一段独立的代码块,它可以被其他代码调用并执行。与函数不同的是,子程序可以不返回任何结果,或者通过参数列表传递结果。在Fortran中,子程序的定义以关键字"SUBROUTINE"开始,后面跟着子程序名和参数列表。子程序体内部可以包含一系列的计算语句,执行完后通过关键字"END SUBROUTINE"结束。

函数实例

exam1:不使用return语句

  • 需要定义返回值
program function_example
    implicit none

    real :: length, width, area
  
    write(*,*) "input length:"
    read(*,*) length
  
    write(*,*) "input width:"
    read(*,*) width

    area = calculate_area(length, width)

    write(*,*) "the area of triangle", area
    read(*,*)

contains

    function calculate_area(length,width) result(area)
        real, intent(in) :: length, width
        real :: area
        area = length * width
    end function calculate_area
  
end program function_example

exam2:使用return语句

  • 直接定义函数(函数返回值)类型
program return_example
    implicit none
    real :: num1, num2, average

    write(*, *) "input two number:"
    read(*, *) num1, num2
  
    average = calculate_average(num1, num2)
  
    write(*,*) "the average of the two numbers:", average
    read(*,*)
    
contains
    real function calculate_average(a, b)
        real, intent(in) :: a, b
        calculate_average = (a + b) / 2.0  
        return  
    end function calculate_average
end program return_example

子程序实例

exam1:Simple Subroutine

program main
    implicit none
    
    call print_message()
    read(*,*)
end program main

subroutine print_message()
    print *, "Hello, World!"
end subroutine print_message

exam2:Subroutine with Arguments

program main
    implicit none
    real :: num1, num2, sum
  
    num1 = 2.5
    num2 = 3.7
    call calculate_sum(num1, num2, sum)
    print *, "The sum is:", sum
    read(*,*)
end program main

subroutine calculate_sum(a, b, result)
    real :: a, b, result
    result = a + b
end subroutine calculate_sum

  • 子程序不是函数,可以理解为按照约定处理一个变量  

exam3:Subroutine with Intent

program main
    implicit none

    real :: numbers(5) = [1,2,3,4,5]
    integer :: i

    call square_array(numbers,5)
  
    do i = 1, 5
        print *, numbers(i)
    end do
    read(*,*)

end program main


subroutine square_array(arr, size)
    integer, intent(in) :: size
    real, intent(inout) :: arr(size)
    
    integer :: i
    
    do i = 1, size
        arr(i) = arr(i) ** 2
    end do
end subroutine square_array
  • 或者
    • 我们添加了一个size函数
program main
    implicit none

    real :: numbers(5) = [1,2,3,4,5]
    integer :: i
    integer :: j

    j = size(numbers)
    call square_array(numbers,j)
  
    do i = 1, j
        print *, numbers(i)
    end do
    read(*,*)

end program main


subroutine square_array(arr, size)
    integer, intent(in) :: size
    real, intent(inout) :: arr(size)
    
    integer :: i
    
    do i = 1, size
        arr(i) = arr(i) ** 2
    end do
end subroutine square_array

函数与子程序的发展史

  • In Fortran, subroutines and functions are used to encapsulate a specific set of instructions that can be called and executed from different parts of a program.
  • Subroutines are defined using the SUBROUTINE keyword followed by a name, and they do not return any values.
    • They are typically used for performing a series of calculations or operations without returning a result. Subroutines can have input parameters, which are variables passed to the subroutine for use within its code. 
  • Functions, on the other hand, are defined using the FUNCTION keyword followed by a name. Functions are used to perform calculations and return a single value as the result.
    • They can have input parameters, similar to subroutines, which are used in the calculation. 
  • Both subroutines and functions can be called from other parts of the program using their respective names.
    • When calling a subroutine, the program flow jumps to the subroutine code, executes it, and then returns to the calling point.
    • When calling a function, the result of the calculation is returned and can be assigned to a variable or used directly in an expression.
  • 在C语言中,存在函数
  • 在Java中,称为方法(method)
  • 在Python中,也有函数(function)
  • 通过区分function和subroutine,Fortran可以更好地满足不同的编程需求。这样的设计可以提高代码的可读性和可维护性,使得程序的逻辑更加清晰明确。
    • Function适用于需要返回结果的计算任务
    • Subroutine适用于需要执行一系列操作或修改参数值的任务。
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

River Chandler

谢谢,我会更努力学习工作的!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值