# Lapack wrappers and final arguments 1

**URL:** https://discourse.julialang.org/t/lapack-wrappers-and-final-arguments-1/124391
**Category:** Internals & Design
**Tags:** lapack
**Created:** [January 2, 2025, 11:33pm UTC](https://discourse.julialang.org/t/lapack-wrappers-and-final-arguments-1/124391 "2025-01-02T23:33:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![juthohaegeman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juthohaegeman/32/8620_2.png) [@juthohaegeman](https://discourse.julialang.org/u/juthohaegeman)
#### Post date: [January 2, 2025, 11:33pm UTC](https://discourse.julialang.org/t/lapack-wrappers-and-final-arguments-1/124391/1 "2025-01-02T23:33:01Z")

</div>

Studying some of the Lapack wrappers over at [LinearAlgebra.jl/src/lapack.jl at master · JuliaLang/LinearAlgebra.jl · GitHub](https://github.com/JuliaLang/LinearAlgebra.jl/blob/master/src/lapack.jl), I am confused by the `ccall` structure. Most relevant lapack methods have `info` as their final argument according to the lapack documentation. However, the `ccall` often adds a number of additional arguments of fixed value `1` and of type `Clong` to the list of arguments, where the number of such arguments ranges is sometimes zero, sometimes one, sometimes two, up to sometimes four.

Why is this necessary and how can I know how many of these extra arguments are needed if I want to wrap other Lapack methods?

---

<div class="post-metadata">

### Author: ![imcinerney](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/imcinerney/32/9882_2.png) [@imcinerney](https://discourse.julialang.org/u/imcinerney)
#### Post date: [January 3, 2025, 12:10am UTC](https://discourse.julialang.org/t/lapack-wrappers-and-final-arguments-1/124391/2 "2025-01-03T00:10:03Z")

</div>

They are known as hidden arguments. Since the LAPACK and BLAS functions are Fortran functions that take some character arguments, the Fortran convention is to pass the length of the character arrays in the arguments as arguments at the end of the function.

Take the [`DGEBAK`](https://netlib.org/lapack/explore-html/de/dfa/group__gebak_gaead827a2b3a8aa69480130c887ee736e.html) function for instance. It has two arguments `JOB` and `SIDE` that are single-length character arrays (so a single character). Therefore, we include [two arguments](https://github.com/JuliaLang/LinearAlgebra.jl/blob/a622302e59a99606936d33dab9da0b51e085e95f/src/lapack.jl#L267) that are just `1` after the info argument to say that the `JOB` and `SIDE` arguments are only of length 1.

---

<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: [January 3, 2025, 12:25am UTC](https://discourse.julialang.org/t/lapack-wrappers-and-final-arguments-1/124391/3 "2025-01-03T00:25:17Z")

</div>

To complement Ian’s answer, you can read the Gfortran documentation about [arguments passing conventions](https://gcc.gnu.org/onlinedocs/gfortran/Argument-passing-conventions.html)

> For arguments of `CHARACTER` type, the character length is passed as a hidden argument at the end of the argument list, except when the corresponding dummy argument is declared as `TYPE(*)`. For deferred-length strings, the value is passed by reference, otherwise by value. The character length has the C type `size_t` (or `INTEGER(kind=C_SIZE_T)` in Fortran). Note that this is different to older versions of the GNU Fortran compiler, where the type of the hidden character length argument was a C `int`. In order to retain compatibility with older versions, one can e.g. for the following Fortran procedure
> 
> ```fortran
> subroutine fstrlen (s, a)
> character(len=*) :: s
> integer :: a
> print*, len(s)
> end subroutine fstrlen
> 
> ```
> 
> define the corresponding C prototype as follows:
> 
> ```c
> #if __GNUC__ > 7
> typedef size_t fortran_charlen_t;
> #else
> typedef int fortran_charlen_t;
> #endif
> 
> void fstrlen_ (char*, int*, fortran_charlen_t);
> 
> ```

Note that this practice is assuming the code we’re calling was compiled with Gfortran (which is the case for the build of openblas shipped with julia) or another compiler with the same calling convention (which should be the case for most common fortran compilers around, in case you wanted to call a different blas/lapack library via libblastrampoline)
