 # Problem creating user-defined function in LibreCalc

I am trying to create the following function:

``````Function IMPARA (x,y)
IMPARA=IMDIV(IMPRODUCT(x,y),IMSUM(x,y))
End Function
``````

When I call the function in the spreadsheet as `=IMPARA(A1,B2)`, I get the following error: BASIC runtime error. Sub-procedure or function procedure not defined.

What am I doing wrong?

Can you use Calc functions in BASIC? It sounds like it may be unhappy with the IMDIV(), IMPRODUCT(), and IMSUM() functions as being ‘not defined’.

I think you are right. Complex numbers are actually strings, so I guess I will have to parse the arguments into real and imaginery parts, convert to numbers and do the math and then re-create the string. PITA.

Depends on operating system. Under windows, “Function Function_Name()” is enough. But under Ubuntu, you must declare the function properly, such “Function Function_Name() as Var_Type”. If your function returning number, use Double data type for output.

@Paijo setting `IMPARA (x as string, y as string) as Var_Type` does not alter the error. I am not sure you can use Calc functions (e.g., SUM) in a user-defined function. As @neolith says, for complex numbers (e.g., “-10+8i”) this makes it a real pain. @neolith please report your results back here, regardless of whether you get it working or not.

@oweng I just try to create a user-defined function use calc function nama. I make a RAND() function that always return value 100. Calc function always executed, and the macro never executed. So, back to main problem, I agree with you that the source of error are undefined function/procedure.

You can call calc functions in basic, but the arguments need to be passed as an array. See this link

I think this function may work–give it a try and confirm.

``````function impara(x,y)
svc=createUnoService("com.sun.star.sheet.FunctionAccess")
arg1=array(x,y)
pr=svc.callFunction("IMPRODUCT",arg1)
su=svc.callFunction("IMSUM",arg1)
arg2=array(pr,su)
impara=svc.callFunction("IMDIV",arg2)
end function``````

Thanks, works perfectly.

I am going to move some of the content from the above comments into an answer in order to provide a possible resolution for this question.

I don’t think you can you use Calc functions in BASIC. Even changing the first line to:

``````IMPARA (x as string, y as string) as Var_Type
``````

…gives the same error. It sounds like it is unhappy with the IMDIV(), IMPRODUCT(), and IMSUM() functions as being ‘not defined’. Complex numbers (e.g., “-10+8i”) are treated as strings, so the function needs to parse the arguments into real and imaginary parts, convert these parts to numbers, do the math, and then re-create the string (answer) to be returned.

A good place to find useful information:
https://wiki.documentfoundation.org/Macros

Here the information on page 513 from Pitonyak guide.

``````**15.10. Accessing Calc functions**
You can call Calc functions from a macro.
Listing 455. Call the MIN function directly.

Sub callFunction
Dim oFA
oFA = createUnoService( "com.sun.star.sheet.FunctionAccess" )
' Calculate min of numbers.
print oFA.callFunction( "MIN", array( 10, 23, 5, 345 ) )
End Sub``````