- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
1 Solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Oh, sorry, I misunderstood your question.
The Fortran 77 way is this:
The Fortran 90 way is this:
There's a Fortran 2003 way as well, with the new PROCEDURE declaration, but that's for another day.
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.
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.)
This has been a standard feature of Fortran since Fortran 77. (In Fortran 66 the syntax was a bit different.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - Steve Lionel (Intel)
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.)
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Oh, sorry, I misunderstood your question.
The Fortran 77 way is this:
The Fortran 90 way is this:
There's a Fortran 2003 way as well, with the new PROCEDURE declaration, but that's for another day.
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page