To know if there is a Julia analogue of some Modern Fortran declarations within subroutines

Hi all,
I would like to know if it is possible to define the intention of the function arguments as in Fortran’s:
intent(in), intent(out) and intent(inout).
I used them a lot in Modern Fortran in order to be sure that the intention inside a subroutine is not changed by mistake. Here in Julia I use functions instead of subroutines, but would like to know if there is a standard method to fix the intention of its arguments.
Thank you very much,
Martín

No, there isn’t. Functions · The Julia Language
Some arguments may be mutable (arrays), but others are immutable. Example:

julia> f(x) = (x = 4)                                                                                                                                                                   
f (generic function with 1 method)                                                                                                                                                      
                                                                                                                                                                                        
julia> x = 5                                                                                                                                                                            
5                                                                                                                                                                                       
                                                                                                                                                                                        
julia> f(x)                                                                                                                                                                             
4                                                                                                                                                                                       
                                                                                                                                                                                        
julia> x                                                                                                                                                                                
5       

Thanks! I have a questions regarding your example. If f(x) returns the expression (x=4) as output, should the results of doing in the repl:
f(3)
and
3=4
be equal? But they aren’t as the former gives just 4 and the latter gives an error message.
Do you know the reason? Best.

The f(x) returns the result of the last expression, which is 4. So, f(x) == 4 should be true.

So f(x)=(x=4) returns just 4, not (x=4), right ?

Correct.

1 Like
x = y

is always assignment, not mutation: Variables · The Julia Language.

In addition to that, Julia, like many other dynamic languages, uses pass-by-sharing, which can briefly summarised as “mutable objects can be mutated, immutable objects cannot”: Functions · The Julia Language. So no, there is no risk of redefining 3=4. In this way you can also see that all immutable arguments are always strictly “in”, all mutable arguments are potentially “inout”. What actually happens depends on what the method does in its body.

3 Likes