type has component which is a procedure pointer to function. The function will return an array. But by calling the pointer , I got trash value and when I tried to find the size of returned array from the call from the procedure pointer, the compiler showed error
" An array-valued argument is required in this context."
If I change function to the form of subroutine, there is no such a problem. Please run the following code and help me with this problem.
compiler : linux+ifort 11.1 intel ifort version. 11.1
-------source code ------------
module type_module
implicit none
type test_type
procedure(fun_interface), nopass, pointer :: fun_ptr
procedure(sub_interface), nopass, pointer :: sub_ptr
end type test_type
abstract interface
function fun_interface(n,x) result(f)
integer, intent(in) :: n
double precision, intent(in) :: x
double precision :: f
end function fun_interface
subroutine sub_interface(n,x,f)
integer, intent(in) :: n
double precision, intent(in) :: x
double precision :: f
end subroutine sub_interface
end interface
contains
subroutine test_type_constructor(test, fun,sub)
interface
function fun(n,x) result(f)
integer, intent(in) :: n
double precision, intent(in) :: x
double precision :: f
end function fun
subroutine sub(n,x,f)
integer, intent(in) :: n
double precision, intent(in) :: x
double precision :: f
end subroutine sub
end interface
type(test_type) :: test
test%fun_ptr => fun;
test%sub_ptr => sub;
end subroutine test_type_constructor
end module type_module
program main
use type_module
type(test_type) :: test
integer :: n =2 ;
double precision :: x(2), f(2)
call test_type_constructor (test, fun1,sub1)
x = (/-1.d0, 1.d0/);
f = 0.d0;
call test%sub_ptr(n,x,f)
print *, " f from call fun ", f;
f = test%fun_ptr(n,x);
print *, " f from call fun ", f;
! Why the following does not work
!print *, " size of returned value from fun_ptr ", size(test%fun_ptr(n,x));
contains
function fun1 (n,x ) result (f )
integer, intent(in) :: n
double precision, intent(in) :: x
double precision :: f
! try this
f = 2.0*x ;
! or this , both give WRONG results
call sub1(n,x,f)
end function fun1
subroutine sub1(n,x,f)
integer, intent(in) :: n
double precision, intent(in) :: x
double precision :: f
f = 2.0*x ;
end subroutine sub1
end program
" An array-valued argument is required in this context."
If I change function to the form of subroutine, there is no such a problem. Please run the following code and help me with this problem.
compiler : linux+ifort 11.1 intel ifort version. 11.1
-------source code ------------
module type_module
implicit none
type test_type
procedure(fun_interface), nopass, pointer :: fun_ptr
procedure(sub_interface), nopass, pointer :: sub_ptr
end type test_type
abstract interface
function fun_interface(n,x) result(f)
integer, intent(in) :: n
double precision, intent(in) :: x
double precision :: f
end function fun_interface
subroutine sub_interface(n,x,f)
integer, intent(in) :: n
double precision, intent(in) :: x
double precision :: f
end subroutine sub_interface
end interface
contains
subroutine test_type_constructor(test, fun,sub)
interface
function fun(n,x) result(f)
integer, intent(in) :: n
double precision, intent(in) :: x
double precision :: f
end function fun
subroutine sub(n,x,f)
integer, intent(in) :: n
double precision, intent(in) :: x
double precision :: f
end subroutine sub
end interface
type(test_type) :: test
test%fun_ptr => fun;
test%sub_ptr => sub;
end subroutine test_type_constructor
end module type_module
program main
use type_module
type(test_type) :: test
integer :: n =2 ;
double precision :: x(2), f(2)
call test_type_constructor (test, fun1,sub1)
x = (/-1.d0, 1.d0/);
f = 0.d0;
call test%sub_ptr(n,x,f)
print *, " f from call fun ", f;
f = test%fun_ptr(n,x);
print *, " f from call fun ", f;
! Why the following does not work
!print *, " size of returned value from fun_ptr ", size(test%fun_ptr(n,x));
contains
function fun1 (n,x ) result (f )
integer, intent(in) :: n
double precision, intent(in) :: x
double precision :: f
! try this
f = 2.0*x ;
! or this , both give WRONG results
call sub1(n,x,f)
end function fun1
subroutine sub1(n,x,f)
integer, intent(in) :: n
double precision, intent(in) :: x
double precision :: f
f = 2.0*x ;
end subroutine sub1
end program