# Weird behavior calling FORTRAN function

**URL:** https://discourse.julialang.org/t/weird-behavior-calling-fortran-function/43764
**Category:** New to Julia
**Tags:** fortran
**Created:** [July 27, 2020, 2:55pm UTC](https://discourse.julialang.org/t/weird-behavior-calling-fortran-function/43764 "2020-07-27T14:55:23Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![AstroBarker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/astrobarker/32/15170_2.png) [@AstroBarker](https://discourse.julialang.org/u/AstroBarker)
#### Post date: [July 27, 2020, 2:55pm UTC](https://discourse.julialang.org/t/weird-behavior-calling-fortran-function/43764/1 "2020-07-27T14:55:23Z")

</div>

I have a Julia function that calls a FORTRAN subroutine that I have written. Everything was working fine until recently I tried to change the return type of the Julia function - an array of the FORTRAN returns instead of a tuple. i.e.

`return x, y, z` → `return hcat(x, y, z)`.

Having changed this, and nothing else, the code now crashes within the FORTRAN routine from an error that would mean the input to the subroutine has somehow changed to bad values. Can someone help me make sense of this?

Edit: The function in question:

```julia
function ComputeDerivatives_Pressure( D::Array{Float64, 1}, E::Array{Float64, 1}, Ne::Array{Float64,1};
    Units_Option::Bool=true )

    # Initialize arrays to hold derivatives
    nx :: Int64 = length( D )
    dPdD :: Array{Float64, 1} = zeros( nx );
    dPdT :: Array{Float64, 1} = zeros( nx );
    dPdY :: Array{Float64, 1} = zeros( nx );
    dPdE :: Array{Float64, 1} = zeros( nx );
    dPdDe :: Array{Float64, 1} = zeros( nx ); 
    dPdTau :: Array{Float64, 1} = zeros( nx ); 

    # =============================================================
    # This calls the FORTRAN function :computederivatives_pressure_
    # FORTRAN compilation mangles the name. Cvoid is the return 
    # type, the next parameters are input types, followed 
    # by the arguements. 
    # =============================================================
    ccall( (:computederivatives_pressure_, "./EoS_jl.so"), Cvoid, 
    ( Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Int64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, Ref{Float64}, 
    Ref{Float64}, Ref{Bool} ), 
    D, E, Ne, nx, dPdD, dPdT, dPdY, dPdE, dPdDe, dPdTau, Units_Option )    

    # return dPdD, dPdT, dPdY, dPdE, dPdDe, dPdTau
    return hcat( copy(dPdD), copy(dPdT), copy(dPdY), copy(dPdE), copy(dPdDe), copy(dPdTau) )

end   

```

---

<div class="post-metadata">

### Author: ![AstroBarker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/astrobarker/32/15170_2.png) [@AstroBarker](https://discourse.julialang.org/u/AstroBarker)
#### Post date: [July 27, 2020, 3:14pm UTC](https://discourse.julialang.org/t/weird-behavior-calling-fortran-function/43764/2 "2020-07-27T15:14:39Z")

</div>

Update: Even without changing the return type, adding a line such as `y = hcat( zeros(nx), zeros(nx), zeros(nx), zeros(nx) )` after the FORTRAN call causes a crash in the FORTRAN.

---

<div class="post-metadata">

### Author: ![jmert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmert/32/3161_2.png) [@jmert](https://discourse.julialang.org/u/jmert)
#### Post date: [July 27, 2020, 3:45pm UTC](https://discourse.julialang.org/t/weird-behavior-calling-fortran-function/43764/3 "2020-07-27T15:45:36Z")

</div>

I think all of your arrays should be passed to Fortran with a `Vector{Float64}` signature, not `Ref{Float64}`. As far as I can tell, the only `Ref` items in the list should be the `nx` and `Units_option` variables, which should also have explicit `Ref` wrappers (assuming it’s still using traditional Fortran calling convention and hasn’t been setup with ISO C bindings instead):

```julia
function ComputeDerivatives_Pressure(D::Array{Float64, 1},
                                     E::Array{Float64, 1},
                                     Ne::Array{Float64,1};
                                     Units_Option::Bool=true)

    # Initialize arrays to hold derivatives
    nx :: Int64 = length( D )
    dPdD :: Array{Float64, 1} = zeros( nx );
    dPdT :: Array{Float64, 1} = zeros( nx );
    dPdY :: Array{Float64, 1} = zeros( nx );
    dPdE :: Array{Float64, 1} = zeros( nx );
    dPdDe :: Array{Float64, 1} = zeros( nx ); 
    dPdTau :: Array{Float64, 1} = zeros( nx ); 

    ccall((:computederivatives_pressure_, "./EoS_jl.so"), Cvoid, 
        (Vector{Float64}, Vector{Float64}, Vector{Float64}, Ref{Int64},
         Vector{Float64}, Vector{Float64}, Vector{Float64}, Vector{Float64},
         Vector{Float64}, Vector{Float64}, Ref{Bool}), 
        D, E, Ne, Ref(nx), dPdD, dPdT, dPdY, dPdE, dPdDe, dPdTau, Ref(Units_Option))

    # return dPdD, dPdT, dPdY, dPdE, dPdDe, dPdTau
    return hcat(dPdD, dPdT, dPdY, dPdE, dPdDe, dPdTau)
end

```

---

<div class="post-metadata">

### Author: ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)
#### Post date: [July 27, 2020, 4:01pm UTC](https://discourse.julialang.org/t/weird-behavior-calling-fortran-function/43764/4 "2020-07-27T16:01:56Z")

</div>

> [@jmert](#):
>
> I think all of your arrays should be passed to Fortran with a `Vector{Float64}` signature, not `Ref{Float64}` .

No, they should be `Ref` (or `Ptr`, which is equivalent), see [Calling fortran function - #8 by simonbyrne](https://discourse.julialang.org/t/calling-fortran-function/2288/8).

---

<div class="post-metadata">

### Author: ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)
#### Post date: [July 27, 2020, 4:08pm UTC](https://discourse.julialang.org/t/weird-behavior-calling-fortran-function/43764/5 "2020-07-27T16:08:22Z")

</div>

Double check that you are using the correct types for `nx` and `Units_Option`: if you’re using gfortran and the default `KIND`s for `INTEGER` and `LOGICAL`, then I think these should both be `Int32`: [KIND Type Parameters (The GNU Fortran Compiler)](https://gcc.gnu.org/onlinedocs/gfortran/KIND-Type-Parameters.html)

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [July 27, 2020, 4:13pm UTC](https://discourse.julialang.org/t/weird-behavior-calling-fortran-function/43764/6 "2020-07-27T16:13:20Z")

</div>

> [@simonbyrne](#):
>
> if you’re using gfortran and the default `KIND` s for `INTEGER` and `LOGICAL` , then I think these should both be `Int32` : [KIND Type Parameters (The GNU Fortran Compiler)](https://gcc.gnu.org/onlinedocs/gfortran/KIND-Type-Parameters.html)

This is also explained in Julia’s documentation: [Calling C and Fortran Code · The Julia Language](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/index.html#man-bits-types-1), see the “System Independent Types” table

---

<div class="post-metadata">

### Author: ![jmert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmert/32/3161_2.png) [@jmert](https://discourse.julialang.org/u/jmert)
#### Post date: [July 27, 2020, 4:18pm UTC](https://discourse.julialang.org/t/weird-behavior-calling-fortran-function/43764/7 "2020-07-27T16:18:40Z")

</div>

> [@simonbyrne](#):
>
> > [@jmert](#):
> >
> > I think all of your arrays should be passed to Fortran with a `Vector{Float64}` signature, not `Ref{Float64}` .
> 
> No, they should be `Ref` (or `Ptr` , which is equivalent), see [Calling fortran function](https://discourse.julialang.org/t/calling-fortran-function/2288/8).

Thanks for correcting me — I misremembered what I’d read [apparently too] long ago in the manual: [Calling C and Fortran Code · The Julia Language](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#When-to-use-T,-Ptr%7BT%7D-and-Ref%7BT%7D-1)
