- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I would like to make a function or subroutine that handles arguments as flexibly as the MAX function. That is, pass in 1 to n individual integer variables . But I have no idea how to declare the variables. All the examples of optional variables I see have optional variables with individual names. Ideally, I would like them assigned to a single dimension array within the function (or subroutine) but passed in as individual values. Its too cumbersome to name (for example) 20 individual variables and check them all with separate if present statements. I need a do loop construct to check if they are present and if so do an operation with them. Is this possible with FORTRAN?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
No. If the variables are all scalars, all integer (or all of the same type) and their position in the argument list doesn't matter, why don't you directly pass in an array instead? [fortran]! In a module somewhere...
FUNCTION my_function(arr) INTEGER, INTENT(IN) :: arr(:) INTEGER :: my_function
!****
! Some fancy operation on an array here...
END FUNCTION
my_function r = my_function([1, 2, 3, some_integer_scalar_variable, ix * iy])[/fortran]
- 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
- 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
I don't think the solution with 2*20 optional arguments need be all that bad, see opt1.f90. You could define a UDT with default initialization and invoke with an extra layer like [fortran]call opt2(t20(label1,var1,label2,var2,label3,var3))[/fortran] see opt2.f90. Or you could use an assumed-shape array of structures like call [fortran]opt3([a1(label1,var1),a1(label2,var2),a1(label3,var3)])[/fortran] see opt3.f90.
- 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
- 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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