Logic Machine Forum
Curious about the operation of this script - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Curious about the operation of this script (/showthread.php?tid=2780)



Curious about the operation of this script - JRP - 07.08.2020

Hello I have this sample script for the definition and call of functions.

Code:
function max(numero1, numero2)     
   if (numero1 > numero2) then     
      resultado = numero1;        
   else                            
      resultado = numero2;        
   end                            

   return resultado;             --
end

log('El valor máximo de la comparación de dos valores es',max(4,10))
log('El valor máximo de la comparación de dos valores es',max(6,5))
It works perfectly regardless of the order of the numbers passed to the function, it always returns the largest.
I have this other variation.
Code:
max = 4, 10
log('El valor máximo de la comparación de dos valores es', max)

function max(numero1, numero2)     

   if (numero1 > numero2) then     
      resultado = numero1;        
   else
      resultado = numero2;
   end
  return(resultado)
end

Here the problem is that if I change the order of the numbers, the function does not return the highest, it seems that it always returns the first.
What could this be due to? Is the way to call the function and pass it the arguments wrong?
a greeting


RE: Curious about the operation of this script - admin - 07.08.2020

You don't need to create a function for this, just use math.max instead. It accepts any number of arguments, not just 2.

As for your second example, you assign 4 to max variable. 10 is not assigned to any variable. You can assign multiple variables this way but you need to specify them like this:
Code:
a, b = 4, 10
log(a, b)



RE: Curious about the operation of this script - JRP - 08.08.2020

Thanks admin for the answer.
The script is only an appearance test for the functions, however thanks for the note from math.max.
Indeed, as soon as I read it, I have noticed what you say, thank you very much.
a greeting