Loading...
You are not logged-in Login/Register





  • Posts   Search Threads
  • brunocaladoJuly 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)July 3, 2009 6:01 PM PDT
    Rate
     
    Re: External Procedure

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


    Steve

    Attaching or including files in a post
    Doctor Fortran blog
    @DoctorFortran on Twitter

    brunocaladoJuly 3, 2009 7:48 PM PDT
    Rate
     
    Re: External Procedure

    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)July 4, 2009 6:20 AM PDT
    Rate
     
    Re: External Procedure

    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.


    Steve

    Attaching or including files in a post
    Doctor Fortran blog
    @DoctorFortran on Twitter

Forum jump:  

Intel Software Network Forums Statistics

16,367 users have contributed to 46,339 threads and 163,926 posts to date.

In the past 24 hours, we have 27 new thread(s) 145 new posts(s), and 83 new user(s).

In the past 3 days, the most popular thread for everyone has been Formula for the intersection of straight lines The most posts were made to Take a look at John Burkhard&# The post with the most views is \"-check none\" generates error

Please welcome our newest member dozo1971


For more complete information about compiler optimizations, see our Optimization Notice.