fortran调用 带有参数 且 返回类型为数组的函数 及 相关歧义分析

1 参考书籍

Fortran95程序设计【彭国伦】 - P189 函数的使用接口

2 fortran环境

Visual Studio 2019(community) + XE2020

3 主要思路

① 定义好待调用的函数(function)

② 编写测试主程序调用该函数

③ 注意 在主程序中使用 被调用的函数 进行声明时, 需要使用interface关键字声明该调用函数

④ 验证结果

4 具体实现代码

4.1 项目结构

项目类型 - Empty Project
(一个fortran控制台程序,示例为FortranEx3项目)
在这里插入图片描述

文件结构 - 涉及两个文件
① 主程序文件 main.f90
② 函数外部文件 Source2.f90
在这里插入图片描述

4.2 主程序文件(入口文件)

main.f90

program test3
    implicit none
    
    !测试调用带有参数的返回类型为数组的函数,需要使用interface关键字
    interface
        function array_test(num)
        double precision,dimension(10) :: array_test
        double precision :: num
        end function
    end interface
        
    
    double precision value
    value = 9.6
    
    print * , array_test(value)

    
    end program

4.3 外部定义的函数文件

Source2.f90

!传入一个实数并对整个数组赋值后返回
function array_test(num)
    double precision,dimension(10) :: array_test
    double precision :: num
    integer count
    do count=1,10
        array_test(count) = num
    end do
    return
end function

4.4 测试结果

成功调用 带有参数的 且 返回值为 数组的 函数 array_test
在这里插入图片描述

5 相关歧义分析

5.1 分析

若在测试程序中(main.f90) 在函数声明时, 不使用 interface 关键字进行相关声明,会导致未调用该函数。

即: 调用函数的代码 会变成 对该函数返回的数组的 某个具体元素的简单调用

原因:

在fortran90语法中,对数组元素的调用为 使用数组名加小括号,如:array_test(1),调用array_test数组的第一个元素。

在Source2.f90程序中,由于此时 函数名array_test被声明为一个数组类型,该函数名fortran90语法既可做 被调用的函数名,又可作返回的数组名

array_test(value)

此段 调用函数的代码,在fortran中

① 既可以指 传入value做参数 并调用该函数, 此时作 函数名

② 又可以指调用该数组的第value个索引的元素, 此时作 数组名

5.2 错误演示代码

main.f90

program test3
    implicit none
    
    !测试调用带有参数的返回类型为数组的函数,需要使用interface关键字
    !interface
    !    function array_test(num)
    !    double precision,dimension(10) :: array_test
    !    double precision :: num
    !    end function
    !end interface
    
    !为方便观察结果而赋予的初值
    double precision,dimension(10) :: array_test = (/0, 1, 2, 3, 4, 5, 6, 7, 8, 9/)
    
    double precision value
    value = 9.6
    
    print * , array_test(9)
    print * , array_test(value)

    
    end program

Source2.f90 代码保持不变

5.3 错误执行结果

在这里插入图片描述
由于array_test被当做 数组名使用,此时value=9.6,

array_test(value)变成简单的调用array_test数组第9个索引的值

而实际的array_test(value)未被当做函数使用

即:未发生为数组每个元素赋值为9.6的过程,最终输出类似 array_test[9] 的效果,返回元素值8。

注:fortran数组下标从1开始。

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Fortran调用C++函数并传递`const char *`参数,需要使用ISO C绑定来实现C++函数Fortran程序之间的互操作性。下面是一个简单的示例,演示了如何在Fortran调用一个C++函数并传递`const char *`参数: C++代码(test.cpp): ```cpp #include <iostream> using namespace std; extern "C" { void my_cpp_function(const char *file_name) { cout << "File name: " << file_name << endl; } } ``` Fortran代码: ```fortran program my_program use iso_c_binding implicit none interface subroutine my_cpp_function_wrapper(c_file_name) bind(C) use iso_c_binding character(c_char), dimension(*), intent(in) :: c_file_name end subroutine my_cpp_function_wrapper end interface character(len=100) :: file_name = "/mnt/d/Code/test.txt" integer(c_intptr_t) :: file_ptr type(c_ptr) :: c_file_ptr file_ptr = c_loc(file_name) c_file_ptr = c_ptr(file_ptr) call my_cpp_function_wrapper(c_file_ptr) ! 其他程序代码 end program my_program ``` Fortran程序中的`my_cpp_function_wrapper`是一个Fortran包装器,用于调用C++函数`my_cpp_function`。在Fortran程序中,需要将字符数组转换为`type(c_ptr)`类型的变量,并将其传递给`my_cpp_function_wrapper`。在Fortran程序中,可以使用`c_loc`函数获取字符数组的地址,并使用`c_ptr`函数将其转换为`type(c_ptr)`类型的变量。 在C++函数中,需要使用`extern "C"`关键字将C++函数声明为C风格的函数,以便Fortran程序可以调用它。在C++函数中,可以直接使用`const char *`类型参数来读取字符串参数的值。 请注意,在使用ISO C绑定时,需要确保字符数组中的字符串以空字符('\0')结尾。这样,C++函数才能正确读取字符串参数的值。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值