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

External Procedure

brunocalado
Beginner
1,026 Views
I saw something really interesting on qsort sample. It is possible to define and pass as argument a procedure which is defined outside the qsort procedure.

Dow do I do this?


PROGRAM
USE IFPORT
integer(2), external :: cmp_function

CALL QSORT(A, size, 4, cmp_function)
END PROGRAM


integer(4) function cmp_function(a1, a2)
integer(4) a1, a2
cmp_function=a2-a1
end function
0 Kudos
1 Solution
Steven_L_Intel1
Employee
1,026 Views
Oh, sorry, I misunderstood your question.

The Fortran 77 way is this:

[plain]subroutine callit (func, arg)
external func
integer func, arg
print *, func(arg)
end subroutine callit[/plain]
When you call callit and pass your own function as the func argument, callit will call your function.

The Fortran 90 way is this:

[plain]subroutine callit (func, arg)
interface
  integer function func (arg)
  integer, intent(in) : arg
  end function func
end interface
integer, intent(in) :: arg
print *, func(arg)
end subroutine callit[/plain]
This is wordier, true, but it provides more information to the compiler. In particular, if callit is in a module, the compiler can check to make sure that the function you pass has the correct interface.

There's a Fortran 2003 way as well, with the new PROCEDURE declaration, but that's for another day.

View solution in original post

0 Kudos
3 Replies
Steven_L_Intel1
Employee
1,026 Views
Well, you did it in this example, though you have declared the function as integer(2) in one place and integer(4) in the other. If the QSORT you are using wants an INTEGER(2) function make sure that you write the function that way.

This has been a standard feature of Fortran since Fortran 77. (In Fortran 66 the syntax was a bit different.)
0 Kudos
brunocalado
Beginner
1,026 Views
Well, you did it in this example, though you have declared the function as integer(2) in one place and integer(4) in the other. If the QSORT you are using wants an INTEGER(2) function make sure that you write the function that way.

This has been a standard feature of Fortran since Fortran 77. (In Fortran 66 the syntax was a bit different.)

I'll fix it. Thank you.

But, how can I create my on procedure that receive a function which is defined outside of the same?

If it is hard to put here, where do I found it?
0 Kudos
Steven_L_Intel1
Employee
1,027 Views
Oh, sorry, I misunderstood your question.

The Fortran 77 way is this:

[plain]subroutine callit (func, arg)
external func
integer func, arg
print *, func(arg)
end subroutine callit[/plain]
When you call callit and pass your own function as the func argument, callit will call your function.

The Fortran 90 way is this:

[plain]subroutine callit (func, arg)
interface
  integer function func (arg)
  integer, intent(in) : arg
  end function func
end interface
integer, intent(in) :: arg
print *, func(arg)
end subroutine callit[/plain]
This is wordier, true, but it provides more information to the compiler. In particular, if callit is in a module, the compiler can check to make sure that the function you pass has the correct interface.

There's a Fortran 2003 way as well, with the new PROCEDURE declaration, but that's for another day.
0 Kudos
Reply