# Create a function that accepts a parameter?

Is it possible to write a function in CL that takes a numerical parameter (or even multiple) and returns a string?
Like if I have variables a=-3 and b=4 that I want to send to it and return some sort of string (intending to send more than one variable to the same function).

function(input):
when input < 0 "\${input}"
when input >0 "+\${input}"
otherwise ""

function(a) would return “-3”
function(b) would return “+4”

I know how to do this by defining two separate variables but wondering if there is a more efficient way with a function like this?

Sorry - not yet. For functions like “format these numbers to output an equation”, there’s also a good chance that could be a built-in function within CL. There are a lot of tricky conditions like “leave this term out if the coefficient is zero” and “leave out the “+” if all the previous terms were left out”. I’d be interested in seeing what you’re working on / trying to do.

If I am understanding your question correctly, I think to use one variable you just need to nest the conditional statements.

function(input):
when input < 0 "\${input}" otherwise when input >0 "+\${input}" otherwise ""

Here is a basic example I slapped together.

With more complex functions, the conditionals can be more annoying with conditionals surrounding negatives, 1, 0 and positives.

I used to have something when I did script work in some google apps scripts around coefficients for equations to try and clean up the math. I had one function for coefficients that would rewrite expressions to subtraction when there were negative coefficients present, eliminate the coefficient when the value was 1 and leave nothing if the constant value was zero. I feel like it’d be super helpful to have a function like that to avoid having to anticipate so many scenarios for outcomes if that makes sense.

function coeff(value, type){
var part = value;
try{var parts=part.split(/[\/]/);}catch(err){var parts=[];}
if(parts.length==2)
{
{
if(value==1){return "";}
else if (value==-1){return "-";}
else {return value;}
}
else if (type=="constant")
{
if(value<0 || eval(value)<0){return " - "+Math.abs(parts[0])+"/"+Math.abs(parts[1]);}
else{return "+"+value;}
}
else if(type=="scientific")
{
value = Math.round(value*100000000)/100000000;
value=value.toExponential()+"";
value=value.replace(/e(\+)?/,"*10^");
return value;
//return value.toExponential().replace(/e(\+)?/,"*10^");
}
else
{
if(value==1||value=="1"){return "+";}
else if(value==-1 || value=="-1"){return " - ";}
else if(value<0 || eval(value)<0){return " - "+Math.abs(parts[0])+"/"+Math.abs(parts[1]);}
else{return "+"+value;}
}
}
else
{
{
if(value==1){return "";}
else if (value==-1){return "-";}
else {return value;}
}
else if (type=="constant")
{
if(value<0 || eval(value)<0){return " - "+Math.abs(value);}
else{return "+"+value;}
}
else if(type=="scientific")
{
value = Math.round(value*100000000)/100000000;
value=value.toExponential()+"";
value=value.replace(/e(\+)?/,"*10^");
return value;
//return value.toExponential().replace(/e(\+)?/,"*10^");
}
else
{
if(value==1||value=="1"){return "+";}
else if(value==-1 || value=="-1"){return " - ";}
else if(value<0 || eval(value)<0){return " - "+Math.abs(value);}
else{return "+"+value;}
}
}
}