- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I would like to know if it is possible to have a generic function returning values of different types, depending on the value it is assigned to.
For instance, the following program will trigger an error with IVF 10.1 . It's obvious I could transform these functions into subroutines (in which case it would work), but I thought the compiler would recognize the type of variable the function result is passed to in the main program.
Olivier
[fortran]PROGRAM MAIN USE MODULE_INTERFACES IMPLICIT NONE INTEGER I REAL(8) R I = GENERIC_FUNCTION(1) R = GENERIC_FUNCTION(1) WRITE(*,*) 'I = ',I WRITE(*,*) 'R = ',R END PROGRAM MAIN MODULE MODULE_INTERFACES IMPLICIT NONE INTERFACE GENERIC_FUNCTION INTEGER FUNCTION IFUN(I) INTEGER,INTENT(IN) :: I END FUNCTION IFUN REAL(8) FUNCTION RFUN(I) INTEGER,INTENT(IN) :: I END FUNCTION RFUN END INTERFACE GENERIC_FUNCTION END MODULE MODULE_INTERFACES INTEGER FUNCTION IFUN(I) IMPLICIT NONE INTEGER,INTENT(IN) :: I IFUN = I END FUNCTION IFUN REAL(8) FUNCTION RFUN(I) IMPLICIT NONE INTEGER,INTENT(IN) :: I RFUN = COS(DBLE(I)) END FUNCTION RFUN [/fortran]
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Because functions are meant for use in expressions, things could get rather confusing if selection on the "use of the function result" was considered. Consider:
[fortran]real :: r(10) ! different kind and rank r = 2 + generic_funtion(1) ! mixed types in expression [/fortran]
[bash]write (*,*) generic_funtion(1) [/bash]
[fortran]interface gen_sub module procedure gen_sub_i ! takes an integer module procedure gen_sub_r ! takes a real end interface gen_sub call gen_sub(generic_function(1)) [/fortran]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes, that is true. Using a generic subroutine to achieve this makes a lot more sense. I have to say though that I have never really liked the mixed arguments feature in expressions (that and the fact that IMPLICIT NONE is optional...). I suppose these are legacy oddities which will persist in the Fortranstandard for decades to come :) ...
Thanks for your answers!
Olivier

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