# Writing to a file or a string with the same function

**URL:** <https://fortran-lang.discourse.group/t/writing-to-a-file-or-a-string-with-the-same-function/8448>\
**Category:** Uncategorized\
**Created:** [August 8, 2024, 11:24am UTC](https://fortran-lang.discourse.group/t/writing-to-a-file-or-a-string-with-the-same-function/8448 "2024-08-08T11:24:13Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [August 8, 2024, 5:34pm UTC](https://fortran-lang.discourse.group/t/writing-to-a-file-or-a-string-with-the-same-function/8448/8 "2024-08-08T17:34:54Z")

</div>

> [@urbanjost](#):
>
> I use a function STR that returns a string and can take up to twenty intrinsic scalars.

Neat. I have sometimes wished to write a Fortran procedure that can take any number of arguments of any type. The built-in `print` statement has this feature. How @urbanjost does it is to declare many `class(*),intent(in),optional` arguments, as shown in his [code](https://github.com/urbanjost/M_msg/blob/master/src/M_msg.F90)

```auto
pure function msg_scalar(generic0, generic1, generic2, generic3, generic4, generic5, generic6, generic7, generic8, generic9, &
                       & generica, genericb, genericc, genericd, generice, genericf, genericg, generich, generici, genericj, &
                       & sep)
implicit none
class(*),intent(in),optional :: generic0, generic1, generic2, generic3, generic4
class(*),intent(in),optional :: generic5, generic6, generic7, generic8, generic9
...

```

His function takes up to 20 arguments, which is more than needed in almost all cases, but a code could be written to generate source code for a function that takes N \> 20 arguments. So with automatic code generation you can get in Fortran some of the flexibility you have in Python.

---

_[View the full topic](https://fortran-lang.discourse.group/t/writing-to-a-file-or-a-string-with-the-same-function/8448)._
