# Ccall problem while calling fortran

**URL:** <https://discourse.julialang.org/t/ccall-problem-while-calling-fortran/58413>\
**Category:** General Usage\
**Tags:** fortran\
**Created:** [April 2, 2021, 5:24am UTC](https://discourse.julialang.org/t/ccall-problem-while-calling-fortran/58413 "2021-04-02T05:24:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_D](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_d/32/22716_2.png) [@Adam\_D](https://discourse.julialang.org/u/Adam_D)\
**Post date:** [April 2, 2021, 5:24am UTC](https://discourse.julialang.org/t/ccall-problem-while-calling-fortran/58413/1 "2021-04-02T05:24:15Z")

</div>

Hi all,

I was playing with calling a stupid fortran code in julia. I encountered some weird results.

1)Here is my fortran code to calculate “volume”.

```julia
module volmod
    implicit none

    
contains
    function volume(x,y,z) result(vol)
        !DEC$ ATTRIBUTES DLLEXPORT :: volume
        use, intrinsic :: iso_fortran_env, only: RK => real64
        real(RK), intent(in) :: x,y,z
        real(RK) :: vol
        vol = x*y*z
    end function volume
end module volmod

```

I, then, created a shared library with

```julia
ifort /dll square3.f90

```

I called this function in julia using

```julia
function volum(DX, DY,DZ)

       product = ccall(("VOLMOD_mp_VOLUME", "C:/Users/<user>/Desktop/recycling_bin/module/square3.dll"),
                    Float64,
                    (Ref{Float32},Ref{Float32},Ref{Float32}),
                    DX,DY,DZ)
    return product
end

rt=volum(4.0,4.0,3.0)

```

It returns 0. If I change the fortran code to

```julia
module volmod
    implicit none

    
contains
    function volume(x,y,z) result(vol)
        !DEC$ ATTRIBUTES DLLEXPORT :: volume
        use, intrinsic :: iso_fortran_env, only: RK => real64
        real(RK), intent(in) :: x,y,z
        real(RK) :: vol
        vol = 100
    end function volume
end module volmod

```

The julia code returns 100 as expected. Any ideas, what is wrong with the first code?

1. Separate from the above issue, whenever I ccall a fortran function that requires a single input argument, I always receive the following error

```julia
LoadError: syntax: ccall argument types must be a tuple; try "(T,)" around c:\Users\<user>\Desktop\recycling_bin\module\julia\square.jl:7

```

“”

Basically, ccall requires me to pass at least two input arguments. Any ideas?

1. Finally, although julia documentation says " `(:function, "library")` and `("function", "library")`" are the same for ccall. :function does not work in my case.

----I am using Julia 1.6 on windows 10 in case it is helpful.

---

<div class="post-metadata">

**Author:** ![Adam\_D](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_d/32/22716_2.png) [@Adam\_D](https://discourse.julialang.org/u/Adam_D)\
**Post date:** [April 2, 2021, 6:12am UTC](https://discourse.julialang.org/t/ccall-problem-while-calling-fortran/58413/2 "2021-04-02T06:12:35Z")

</div>

> Blockquote The julia code returns 100 as expected. Any ideas, what is wrong with the first code?

I figured out my error. Input type must have been Ref{Float64} not 32. That answers my first question.

---

<div class="post-metadata">

**Author:** ![magister-ludi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/magister-ludi/32/4003_2.png) [@magister-ludi](https://discourse.julialang.org/u/magister-ludi)\
**Post date:** [April 2, 2021, 6:30am UTC](https://discourse.julialang.org/t/ccall-problem-while-calling-fortran/58413/3 "2021-04-02T06:30:22Z")

</div>

> [@Adam\_D](#):
>
> Separate from the above issue, whenever I ccall a fortran function that requires a single input argument, I always receive the following error

The error message is telling you.

```julia
LoadError: syntax: ccall argument types must be a tuple; try "(T,)" around c:\Users\<user>\Desktop\recycling_bin\module\julia\square.jl:7

```

Although your function only has one argument, the Julia interface requires the argument typess to be a **tuple**. So in this case you need a one-tuple

```julia
ccall(("function_name", "dll_to_link_to"),
       Float64,
       (argument_type,), argument)

```

instead of

```julia
ccall(("function_name", "dll_to_link_to"),
       Float64,
       (argument_type), argument)

```

`(argument_type)` is **not** a tuple, `(argument_type,)` **is** a tuple. EDIT: In case it’s not obvious, there’s an extra comma in the expression that is a tuple.

Sorry I can’t help with your other questions.

---

<div class="post-metadata">

**Author:** ![Adam\_D](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_d/32/22716_2.png) [@Adam\_D](https://discourse.julialang.org/u/Adam_D)\
**Post date:** [April 2, 2021, 6:59am UTC](https://discourse.julialang.org/t/ccall-problem-while-calling-fortran/58413/4 "2021-04-02T06:59:55Z")

</div>

Thank you sir! This helps a lot!
