NickFort
Technical User
- Jun 10, 2010
- 113
Hi there,
I'm just playing around with derived data types, and so I made some functions to see how to pass them to different program units. So, for example, I define a type "vector", which has an "x" and a "y" component, which I pass to a function which simply adds the "x" and the "y" and outputs that.
So, I have the following for the driver program:
and this as the function "addthevecs":
Having provided an explicit interface, I would expect the above to compile and run, but it doesn't. In gfortran, I get the following error:
g95 gives more or less the same information:
However, when I put the definition of the derived type in a module "typemod", it compiles and runs fine:
Now, I would expect both the explicit interface and the module approach to work, but only the latter does. Why is this? Or have I done something wrong with the explicit interface?
Thanks!
NickFort
--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
I'm just playing around with derived data types, and so I made some functions to see how to pass them to different program units. So, for example, I define a type "vector", which has an "x" and a "y" component, which I pass to a function which simply adds the "x" and the "y" and outputs that.
So, I have the following for the driver program:
Code:
program test_derived
implicit none
interface
real function addthevecs(myvec)
implicit none
type :: vector
real :: x
real :: y
end type
type(vector) :: myvec
end function
end interface
type :: vector
real :: x
real :: y
end type
type(vector) :: myvec
real :: mysum
myvec%x = 3.
myvec%y = 4.
mysum = addthevecs(myvec)
print *, mysum
end program
and this as the function "addthevecs":
Code:
real function addthevecs(myvec)
implicit none
type :: vector
real :: x
real :: y
end type
type(vector) :: myvec
addthevecs = myvec%x + myvec%y
end function
Having provided an explicit interface, I would expect the above to compile and run, but it doesn't. In gfortran, I get the following error:
Code:
>> gfortran test_derived.f95 addthevecs.f95 -o test
test_derived.f95:27.19:
mysum = addthevecs(myvec)
1
Error: Type mismatch in argument 'myvec' at (1); passed TYPE(vector) to TYPE(vector)
g95 gives more or less the same information:
Code:
>> g95 test_derived.f95 addthevecs.f95 -o test
In file test_derived.f95:27
mysum = addthevecs(myvec)
1
Error: Type mismatch in parameter 'myvec' at (1). TYPE(vector) is not the same type between formal/actual
However, when I put the definition of the derived type in a module "typemod", it compiles and runs fine:
Code:
module typemod
type :: vector
real :: x
real :: y
end type
end module
Code:
program test_derived
use typemod
implicit none
type(vector) :: myvec
real :: mysum, addthevecs
myvec%x = 3.
myvec%y = 4.
mysum = addthevecs(myvec)
print *, mysum
end program
Code:
real function addthevecs(myvec)
use typemod
implicit none
type(vector) :: myvec
addthevecs = myvec%x + myvec%y
end function
Now, I would expect both the explicit interface and the module approach to work, but only the latter does. Why is this? Or have I done something wrong with the explicit interface?
Thanks!
NickFort
--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.