[英]Calling Fortran functions which return KIND=10 complex values from C++
[英]Fortran functions and return values
如何在Fortran中編寫一個函數,它將輸入和輸出都作為參數? 例如:
fun(integer input,integer output)
我想利用輸出值。 我嘗試過這樣的東西,但輸出變量沒有保持該值。
具體來說,我從Fortran調用一個C函數,它將輸入和輸出作為參數。 我能夠成功傳遞輸入值,但輸出變量沒有獲取值。
你的樂趣()就像我這樣的Fortran程序員稱之為SUBROUTINE(是的,我們也在Fortran-town中大喊我們的關鍵字)。 FUNCTION是一個像這樣的值返回的東西:
sin_of_x = sin(x)
因此,您的第一個決定是您的Fortran代碼采用哪種方法。 您可能想要使用SUBROUTINE。 然后整理你的參數的INTENT。
一個例子。 如果你想要一個返回void的函數,你應該使用一個子程序。
function foo(input, output)
implicit none
integer :: foo
integer, intent(in) :: input
integer, intent(out) :: output
output = input + 3
foo = 0
end function
program test
implicit none
integer :: a, b, c, foo
b = 5
a = foo(b, c)
print *,a,b, c
end program
如果您正在調用C例程,則簽名將使用引用。
$ cat test.f90
program test
implicit none
integer :: a, b, c, foo
b = 5
a = foo(b, c)
print *,a,b, c
end program
$ cat foo.c
#include <stdio.h>
int foo_(int *input, int *output) {
printf("I'm a C routine\n");
*output = 3 + *input;
return 0;
}
$ g95 -c test.f90
$ gcc -c foo.c
$ g95 test.o foo.o
$ ./a.out
I'm a C routine
0 5 8
如果你使用字符串,事情會變得混亂。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.