Function returning difference from 2 Variables Topic is solved
Function returning difference from 2 Variables
Hi
I am looking for a simple function returning difference Value in two Variables
for example:
Variable1= 10
Variable2= 15
Returning +5
Variable1= 20
Variable2= 10
Returning -10
thanks for help
I am looking for a simple function returning difference Value in two Variables
for example:
Variable1= 10
Variable2= 15
Returning +5
Variable1= 20
Variable2= 10
Returning -10
thanks for help
- flyingDman
- Posts: 2817
- Joined: 29 Sep 2013, 19:01
Re: Function returning difference from 2 Variables
Look at this: https://www.autohotkey.com/docs/Functions.htm#intro. You're almost there...
14.3 & 1.3.7
Re: Function returning difference from 2 Variables
Yes I have already read this and tried some things, but I never got it to work, that's why I ask her for help
maybe someone can tell me what I do wrong
maybe someone can tell me what I do wrong
Code: Select all
difference(val1, val2)
{
if val2<val1
return val1 - val2
if val1<val2
return val2 - val1
}
Re: Function returning difference from 2 Variables
Not sure why you need a function, but it is:
val2 val1
15 - 10 = 5
10 - 20 = -10
Maybe i misunderstand something else...
Code: Select all
difference(val1, val2)
{
return val2 - val1
}
15 - 10 = 5
10 - 20 = -10
Maybe i misunderstand something else...
- flyingDman
- Posts: 2817
- Joined: 29 Sep 2013, 19:01
Re: Function returning difference from 2 Variables
see https://www.autohotkey.com/docs/commands/IfExpression.htm
But if you want the absolute value, use https://www.autohotkey.com/docs/commands/Math.htm#Abs
But if you want the absolute value, use https://www.autohotkey.com/docs/commands/Math.htm#Abs
14.3 & 1.3.7
Re: Function returning difference from 2 Variables
Would https://biga-ahk.github.io/biga.ahk/#/?id=subtract help?
Code: Select all
A := new biga() ; requires https://www.npmjs.com/package/biga.ahk
msgbox, % A.subtract(15, 10)
; => 5
msgbox, % A.subtract(10, 20)
; => -10
Re: Function returning difference from 2 Variables
Thanks all for your help
my solution is now this, works great:
my solution is now this, works great:
Code: Select all
number1 := 10
number2 := 15
tooltip, % diferenceval := diference(number1,number2)
sleep, 2000
;;;;;;;;;function;;;;;;;;;;
diference(val1, val2)
{
if val1<val2
return val2 - val1
if val2<val1
return val1 - val2
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;
Re: Function returning difference from 2 Variables
I don't think it does what you intended... try with number1 greater than number2 (you will get negative results). Or, if negative results are okay, it's working by chance like you intended, but you still have redundant code (and you should try Lepes' code again).
and then look at the docs of If again. For expression syntax, you'll need parentheses with If:
if (val1<val2) and if (val2<val1)
You could also think about using the Abs() function, if the result should always be positive (your original post says otherwise, but your latest code looks like it).
and then look at the docs of If again. For expression syntax, you'll need parentheses with If:
if (val1<val2) and if (val2<val1)
You could also think about using the Abs() function, if the result should always be positive (your original post says otherwise, but your latest code looks like it).
Code: Select all
diference(val1, val2)
{
return abs(val2 - val1)
}
Re: Function returning difference from 2 Variables
Tried abs, but that's not what I need
I want to know if the second number get bigger or lower, like 10to15=+5 and 15to10=-5
and to "For expression syntax", well I tried first with () and it did not work,
so I removed them, and it works
all I can say is my function does what I want
I want to know if the second number get bigger or lower, like 10to15=+5 and 15to10=-5
and to "For expression syntax", well I tried first with () and it did not work,
Code: Select all
;;;;;;;;;function(not work);;;;;;;;;;
diference(val1, val2)
{
if (val1<val2)
return val2 - val1
if (val2<val1)
return val1 - val2
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;
Code: Select all
;;;;;;;;;function(works);;;;;;;;;;
diference(val1, val2)
{
if val1<val2
return val2 - val1
if val2<val1
return val1 - val2
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;
Re: Function returning difference from 2 Variables
Well, your function doesn't really look like it (which would always return non-negative numbers, if the if-syntax wasn't wrong.) What do you then need the Ifs for? Especially the second one (since the first is always true) ?
Anyway, then simply use viewtopic.php?f=76&t=97309&p=432349#p432333 (or variables vice versa, however you prefer).
Lucky guy Look out for the actual (correct) syntax, if you plan to do more with AHK.all I can say is my function does what I want
Re: Function returning difference from 2 Variables
Yes Greg
you are right, I have tested it now without the ifs and its same outcome
well I am not understand how it works like this, maybe you can explain it?
you are right, I have tested it now without the ifs and its same outcome
Code: Select all
diference(val1, val2)
{
return val2 - val1
return val1 - val2
}
Re: Function returning difference from 2 Variables Topic is solved
It's still redundant. This would be enough
since the second return line will never be reached in your code. The function always returns after the first return.
This code above simply subtracts val1 from val2, always, which seems to be exactly what you wanted... it's a simple subtraction.
Substitute val1 and val2 for the actual values to see whats happening:
diference(10,50) returns 40 : return 50 - 10
diference(50,10) returns -40 : return 10 - 50
Code: Select all
msgbox % diference(10,50)
msgbox % diference(50,10)
diference(val1, val2)
{
return val2 - val1
}
This code above simply subtracts val1 from val2, always, which seems to be exactly what you wanted... it's a simple subtraction.
Substitute val1 and val2 for the actual values to see whats happening:
diference(10,50) returns 40 : return 50 - 10
diference(50,10) returns -40 : return 10 - 50
Re: Function returning difference from 2 Variables
I got it now, It's less difficult than I thought
thanks for your help
thanks for your help
- flyingDman
- Posts: 2817
- Joined: 29 Sep 2013, 19:01
Re: Function returning difference from 2 Variables
Change + into -flyingDman wrote: ↑01 Dec 2021, 01:03Look at this: https://www.autohotkey.com/docs/Functions.htm#intro. You're almost there...
14.3 & 1.3.7