# How to Get Outputs from ccall

**URL:** <https://discourse.julialang.org/t/how-to-get-outputs-from-ccall/42864>\
**Category:** General Usage\
**Created:** [July 10, 2020, 6:42pm UTC](https://discourse.julialang.org/t/how-to-get-outputs-from-ccall/42864 "2020-07-10T18:42:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![duncanam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/duncanam/32/10669_2.png) [@duncanam](https://discourse.julialang.org/u/duncanam)\
**Post date:** [July 10, 2020, 6:42pm UTC](https://discourse.julialang.org/t/how-to-get-outputs-from-ccall/42864/1 "2020-07-10T18:42:40Z")

</div>

Hi, I’m trying to port some MATLAB code over to Julia that relies on some of MATLAB’s `calllib()` functions. However, I am confused how I am supposed to get multiple outputs out of Julia’s `ccall()` function as we are only able to define the type of a single output but are required to type all the inputs, so this tells me that this is not possible.

Here is an example of the issue. A MATLAB call looks like this:

```matlab
libname = 'refprop';
T = 300.0;
P_rp = 101325;
z = 1;
herr = 32*ones(255);
 [~,~,~,D_rp,Dl,Dv,x,y,q,e,h,s,cv,cp,w,ierr,errTxt] = calllib(libName,'TPFLSHdll', T, P_rp, z, 0, 0, 0, zeros(1,numComponents), zeros(1,numComponents), 0, 0, 0, 0, 0, 0, 0, 0, herr, 255);

```

In Julia, I do:

```julia
T = 300.0;
P_rp = 101325;
z = 1;
# herr = 32*ones(255); # for some reason passing this breaks everything, but that's another problem
D_rp = Cdouble;
Dl = Cdouble;
Dv = Cdouble;
val = ccall((:TPFLSHdll,:REFPRP64),Cdouble,(Cdouble,Cdouble,Cdouble,Cdouble,Cdouble,Cdouble,Ptr{Cdouble},Ptr{Cdouble}, Cdouble, Cdouble, Cdouble, Cdouble, Cdouble, Cdouble, Cdouble, Cdouble, Ptr{Cdouble}, Cdouble),T,P_rp,z,0,0,0,zeros(1),zeros(1),0,0,0,0,0,0,0,0,32*ones(255),255);

```

And this gives me `val = 0.0`, and I don’t know how to access the variables such as `D_rp`. Is this even possible in Julia or do I have to stay in MATLAB?

By the way, the documentation of the TPFLSHdll library with inputs and outputs (?) is on the bottom of document page 124 /pdf page 128 [HERE](https://www.nist.gov/system/files/documents/2018/05/23/refprop10a.pdf).

---

<div class="post-metadata">

**Author:** ![GregR](https://avatars.discourse-cdn.com/v4/letter/g/e68b1a/32.png) [@GregR](https://discourse.julialang.org/u/GregR)\
**Post date:** [July 10, 2020, 6:57pm UTC](https://discourse.julialang.org/t/how-to-get-outputs-from-ccall/42864/2 "2020-07-10T18:57:26Z")

</div>

Try this

[https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#Passing-Pointers-for-Modifying-Inputs-1](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#Passing-Pointers-for-Modifying-Inputs-1)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 10, 2020, 8:10pm UTC](https://discourse.julialang.org/t/how-to-get-outputs-from-ccall/42864/3 "2020-07-10T20:10:45Z")

</div>

This looks like a Fortran library, so the following note from the manual here: [Calling C and Fortran Code · The Julia Language](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/) applies:

> When calling Fortran, all inputs must be passed by pointers to heap- or stack-allocated values, so all type correspondences above should contain an additional `Ptr{..}` or `Ref{..}` wrapper around their type specification.

There’s more information about wrapping Fortran subroutines here: [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)

And another related question here: [Calling fortran function - #8 by simonbyrne](https://discourse.julialang.org/t/calling-fortran-function/2288/8)

Basically, all of your input types in the `ccall` should be `Ref{Cdouble}`, `Ref{Cint}`, etc. The return type must be `Cvoid` because Fortran subroutines don’t return anything in the sense we’re talking about.

I would suggest starting with a _much_ simpler example to get an idea of what’s going on. If you can write your own Fortran code, start with a subroutine with one argument and make sure you can call it correctly, then try a subroutine with one input and one output. If you can’t, then maybe you can start with a simpler function in the same library, just to get a sense of what’s going on.
