# Function won't compile. Is there a workaround? [duplicate]

This question already has an answer here:

Here's a simplified form of a function I would like to compile, but it produces errors

DataType = Compile[{{Inputdata, _Real, 1}}, Module[{CIEL, CIEa, CIEb},
dim = Dimensions@Inputdata;
If[dim[[1]] > 3,
{CIEL, CIEa, CIEb} = Inputdata
]
],
CompilationTarget -> "C"
];


The error Mathematica produces is:

Compile::extscalar: dim=Dimensions[Inputdata] cannot be compiled and will be evaluated externally. The result is assumed to be of type Void. >>

Is there a workaround to allow these types of functions to compile for conditionals that depend on the dimensions of a list?

-

## marked as duplicate by Oleksandr R., Sjoerd C. de Vries, m_goldberg, Yves Klett, ciaoApr 27 '14 at 20:39

You are setting global variables from inside the compiled function, I don't think that is possible, is that your intention? What should the function return? –  s0rce Nov 27 '13 at 2:55
@s0rce Nah, fixed that. Looks like it should now. –  R Hall Nov 27 '13 at 3:12
If (Dimensions@Inputdata)[[1]] > 3 then you can't set {CIEL, CIEa, CIEb} = Inputdata since Dimensions@{CIEL, CIEa, CIEb} == {3}. –  s0rce Nov 27 '13 at 3:21
@s0rce The error is above this part of the function. so change that to whatever and the error is the same –  R Hall Nov 27 '13 at 3:37
It compiles for me, try a fresh kernel. What version are you using, I'm using 9. –  s0rce Nov 27 '13 at 3:42

Conditionals are fine in compiled functions...

DataType = Compile[{{Inputdata, _Real, 1}},
If[Length@Inputdata > 3, 1, 0], CompilationTarget -> "C"];

DataType[{5, 6, 7}]

(* 0 *)

DataType[{5, 6, 7, 8}]

(* 1 *)


The problem is your function returns nothing, you are trying to set global variables from inside the compiled function and also you never use a, b, and c.

-
Length won't work in this case. I changed the variable names at the last minute and just chose the wrong names. fixed –  R Hall Nov 27 '13 at 3:08
Isn't Length equivalent to the first element in the output of Dimensions? –  s0rce Nov 27 '13 at 3:19
It is but because dim is used multiple times I need Dimensions. –  R Hall Nov 27 '13 at 3:36