Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28632 Discussions

Using TYPE arguments in the external function

morethanair
Beginner
635 Views
Hi, all

I got some problem to use the external function.

I tested it with a simple integer argument, and it worked.
----------------------------------------------------------------------
program PassArg

implicit none

! Variables
integer::i,ret
interface
integer function passtest(d)
integer::d
end function passtest
end interface
! Body of PassArg

i=1
ret=passtest(i)

print *,ret

end program PassArg

integer function passtest(d)
implicit none
integer::d
passtest=d+6

end function passtest
----------------------------------------------------------------

After then, I tested the program with the TYPE argument.

It's almost same, but it didn't work.

--------------------------------------------------------------------
program PassArg

implicit none

! Variables
integer::ret
type pass
integer::i
integer::j
end type pass
type(pass)::pp
interface
integer function passtest(d)
type pass
integer::i
integer::j
end type pass
type(pass)::d
end function passtest
end interface
! Body of PassArg

pp%i=1
pp%j=1

ret=passtest(pp)

print *,ret

end program PassArg

integer function passtest(d)
implicit none
type pass
integer::i
integer::j
end type pass
type(pass)::d
passtest=d%i+d%j

end function passtest
-----------------------------------------------------------------------------------

It has two errors like below.

1. error #8000: There is a conflict between local interface block and external interface block. F:\My Projects\PassArg\PassArg\PassArg.f90 27
2. error #6633: The type of the actual argument differs from the type of the dummy argument. [PP] F:\My Projects\PassArg\PassArg\PassArg.f90 40

I'm a newbie of FORTRAN, so I don't know what's wrong here.

Is there some special rule to use TYPE as an argument?

Please tell me some solutions...

Thank you so much
0 Kudos
1 Solution
onkelhotte
New Contributor II
635 Views
When you put the declaration of pass into a module it works.

I think that your mistake was declaring pass twice. Im not sure why this dont work, maybe someone else can explain it to us :-)

Markus

[cpp]module passDeclaration
    type pass
        integer::i
        integer::j
    end type pass
end module passDeclaration

!   ***********************

program PassArg
    use passDeclaration
    implicit none

    ! Variables
    integer::ret, passtest
    type(pass)::pp

    ! Body of PassArg
    pp%i=1
    pp%j=1
    ret=passtest(pp)
    print *,ret
end program PassArg

!   ***********************

integer function passtest(d)
    use passDeclaration
    implicit none 
    type(pass)::d
    
    passtest=d%i+d%j
end function passtest[/cpp]

View solution in original post

0 Kudos
6 Replies
onkelhotte
New Contributor II
636 Views
When you put the declaration of pass into a module it works.

I think that your mistake was declaring pass twice. Im not sure why this dont work, maybe someone else can explain it to us :-)

Markus

[cpp]module passDeclaration
    type pass
        integer::i
        integer::j
    end type pass
end module passDeclaration

!   ***********************

program PassArg
    use passDeclaration
    implicit none

    ! Variables
    integer::ret, passtest
    type(pass)::pp

    ! Body of PassArg
    pp%i=1
    pp%j=1
    ret=passtest(pp)
    print *,ret
end program PassArg

!   ***********************

integer function passtest(d)
    use passDeclaration
    implicit none 
    type(pass)::d
    
    passtest=d%i+d%j
end function passtest[/cpp]
0 Kudos
Steven_L_Intel1
Employee
635 Views
You have two different definitions of type PASS, as far as the Fortran language is concerned, even though they have the same name and components. You probably inserted the declaration inside the INTERFACE block because if you didn't, you got an error that the type was not declared. What you should have done was add IMPORT to the interface, like this:
[cpp]interface
integer function passtest(d)
import
type(pass)::d
end function passtest
end interface[/cpp]
But this would still give you an error if you built this in Visual Studio because generated interface checking would note again that the type as declared in passtest was a different declaration of PASS than the one in the caller.

Using a module is one approach, another is to make passtest a contained routine, like this:

[cpp]program PassArg

implicit none

! Variables
integer::ret
type pass
integer::i
integer::j
end type pass
type(pass)::pp

! Body of PassArg

pp%i=1
pp%j=1

ret=passtest(pp)

print *,ret

contains

integer function passtest(d)
implicit none
type(pass)::d
passtest=d%i+d%j

end function passtest

end program PassArg
[/cpp]
Now you don't need an INTERFACE block and don't need to redeclare the type.

For further reading, see my Doctor Fortran articles Doctor Fortran Gets Explicit and Domestic or Imported?
0 Kudos
morethanair
Beginner
635 Views

Thanks a lot.

I'll try this.

0 Kudos
morethanair
Beginner
635 Views

I tried it with the module, but still got the error

error 1 error #6633: The type of the actual argument differs from the type of the dummy argument. [PP] F:My ProjectsPassArgPassArgPassArg.f90 34

Can someone let me know what the problem is?

Thank you.

0 Kudos
Steven_L_Intel1
Employee
635 Views
Do a Build > Rebuild of your project. If you still have problems, post the code you are using.
0 Kudos
morethanair
Beginner
635 Views
Do a Build > Rebuild of your project. If you still have problems, post the code you are using.


I forgot to rebuild. Thank you so much.

Now it works well!!
0 Kudos
Reply