Forum Jump

Select Group :
Select Forum :
Sorted By :
Sort Order :
From The :
 
Thread Tools  Search this thread 
brunocalado
Total Points:
561
Status Points:
61
Brown Belt
July 3, 2009 5:30 PM PDT
External Procedure
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
Steve Lionel (Intel)
Total Points:
112,121
Status Points:
112,121
Black Belt
July 3, 2009 6:01 PM PDT
Rate
 
#1
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.)



brunocalado
Total Points:
561
Status Points:
61
Brown Belt
July 3, 2009 7:48 PM PDT
Rate
 
#2 Reply to #1
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?


Steve Lionel (Intel)
Total Points:
112,121
Status Points:
112,121
Black Belt
July 4, 2009 6:20 AM PDT
Rate
 
|Best Answer
#3 Reply to #2
Oh, sorry, I misunderstood your question.

The Fortran 77 way is this:

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

The Fortran 90 way is this:

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
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.





Intel Software Network Forums Statistics

8290 users have contributed to 31236 threads and 99111 posts to date.
In the past 24 hours, we have 7 new thread(s) 19 new posts(s), and 24 new user(s).

In the past 3 days, the most popular thread for everyone has been comparison cilk++, openmp, pthreads first results The most posts were made to comparison cilk++, openmp, pthreads first results The post with the most views is Very amusing...  Escalated as

Please welcome our newest member zq.x