title: Julia: using `Ptr` or `Ref` in combination with ccall and `cfunction`
body: <p>My main question is about using pointer to modify content in Julia. I know Julia by itself has this immutable notion that allows for arrays for example to be passed by reference, and if the content of the array is changed properly in a function then we can expect the changed values outside of a function too. But I wanted to ask more about the integration of <code>cfunction</code> and <code>ccall</code> using <code>Ref{T}</code> for changing the content which make variable changes to be visible outside of a specific loop or function.</p>
  
  <p>This is with reference to this topic in the documentation:<a href="http://julia-demo.readthedocs.io/en/stable/manual/calling-c-and-fortran-code.html#passing-pointers-for-modifying-inputs" rel="nofollow noreferrer">Passing pointers for modifying inputs</a></p>
  
  <p>I was wondering how it is possible to use a Julia defined function to be used in this way. I thought <code>cfunction</code> is something that I can convert my Julia functions to C-understandable code, and then I can call them by <code>ccall</code>, however I was not successful doing that. Consider the following:</p>
  
  <pre><code>function foo(x::Ref{Cint})
      x+=convert(Cint,2)
      return x::Cint
  end
  
  y=Ref{Cint}(convert(Cint,2))
  c_foo = cfunction(foo, Ref{Cint}, (Ref{Cint},),y)
  ccall(:c_foo, Ref{Cint}, (Ref{Cint},), y)
  </code></pre>
  
  <p>And for cfunction I get:</p>
  
  <pre><code>MethodError: no method matching cfunction(::#foo, ::Type{Ref{Int32}}, ::Tuple{DataType}, ::Base.RefValue{Int32})
  </code></pre>
  
  <p>All I want to do is to have a variable <code>y = 2</code> and inside a function add <code>2</code> more, so outside of the function if I print the value of <code>y</code>, I get <code>4</code>. (I do not mean the answers provided by the arrays.)</p>
  
  <p>Another question I have is that upon solving this question, would it be possible to also use self defined Julia types for this as well?</p>