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

Can a procedure argument be optional?

eos_pengwern
Beginner
426 Views
I have in my program a subroutine of the form...

[bash]    subroutine MapCentre(fitobj, centre_x, centre_y, sidelength, image, callback, c_object, error)
                
        type(FitObject), intent(inout), target :: fitobj
        integer, intent(in) :: centre_x, centre_y, sidelength
        integer(4), intent(out), dimension(:,:), target :: image
        type(c_ptr), intent(in) :: c_object
        integer, optional, intent(out) :: error
        interface 
            subroutine callback(c_object) bind(c)  ! N.B. 'bind(c)' implies 'external'
                use, intrinsic :: iso_c_binding
                type(c_ptr), intent(in) :: c_object
            end subroutine callback
        end interface

        ...

    end subroutine MapCentre[/bash]


I'd like to make the arguments 'image', 'callback' and 'c_object' to be optional. It's obvious enough how to do this with the parameter arguments, but where would I put the word 'optional' within the procedure interface? Is this even possible?

Stephen.
0 Kudos
1 Solution
Steven_L_Intel1
Employee
426 Views
You do it with an abstract interface and PROCEDURE. Like this:

[fortran]    abstract interface 
        subroutine callback_int(c_object) bind(c)  ! N.B. 'bind(c)' implies 'external'  
        use, intrinsic :: iso_c_binding  
        type(c_ptr), intent(in) :: c_object  
        end subroutine callback_int
    end interface  

...

   subroutine MapCentre(fitobj, centre_x, centre_y, sidelength, image, callback, c_object, error)  
      use, intrinsic :: iso_c_binding                
        type(FitObject), intent(inout), target :: fitobj  
        integer, intent(in) :: centre_x, centre_y, sidelength  
        integer(4), intent(out), dimension(:,:), target :: image  
        type(c_ptr), intent(in) :: c_object  
        integer, optional, intent(out) :: error  
        procedure(callback_int), optional :: callback
      
    end subroutine MapCentre  [/fortran]

View solution in original post

0 Kudos
2 Replies
Steven_L_Intel1
Employee
427 Views
You do it with an abstract interface and PROCEDURE. Like this:

[fortran]    abstract interface 
        subroutine callback_int(c_object) bind(c)  ! N.B. 'bind(c)' implies 'external'  
        use, intrinsic :: iso_c_binding  
        type(c_ptr), intent(in) :: c_object  
        end subroutine callback_int
    end interface  

...

   subroutine MapCentre(fitobj, centre_x, centre_y, sidelength, image, callback, c_object, error)  
      use, intrinsic :: iso_c_binding                
        type(FitObject), intent(inout), target :: fitobj  
        integer, intent(in) :: centre_x, centre_y, sidelength  
        integer(4), intent(out), dimension(:,:), target :: image  
        type(c_ptr), intent(in) :: c_object  
        integer, optional, intent(out) :: error  
        procedure(callback_int), optional :: callback
      
    end subroutine MapCentre  [/fortran]
0 Kudos
eos_pengwern
Beginner
426 Views
Looks like just the ticket. Thank you Steve.
0 Kudos
Reply