Function returning difference from 2 Variables Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
armin889
Posts: 96
Joined: 02 Nov 2021, 15:11

Function returning difference from 2 Variables

01 Dec 2021, 00:48

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
armin889
Posts: 96
Joined: 02 Nov 2021, 15:11

Re: Function returning difference from 2 Variables

01 Dec 2021, 01:19

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

Code: Select all

difference(val1, val2)
{
 if val2<val1
  return val1 - val2  
     if val1<val2
  return val2 - val1  
}
Lepes
Posts: 141
Joined: 06 May 2021, 07:32
Location: Spain

Re: Function returning difference from 2 Variables

01 Dec 2021, 02:02

Not sure why you need a function, but it is:

Code: Select all

difference(val1, val2)
{
  return val2 - val1  
} 
val2 val1
15 - 10 = 5
10 - 20 = -10

Maybe i misunderstand something else...
User avatar
Chunjee
Posts: 1402
Joined: 18 Apr 2014, 19:05
Contact:

Re: Function returning difference from 2 Variables

01 Dec 2021, 02:08

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
armin889
Posts: 96
Joined: 02 Nov 2021, 15:11

Re: Function returning difference from 2 Variables

01 Dec 2021, 02:18

Thanks all for your help

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 
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Function returning difference from 2 Variables

01 Dec 2021, 02:39

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).

Code: Select all

diference(val1, val2)
{
  return abs(val2 - val1) 
}
armin889
Posts: 96
Joined: 02 Nov 2021, 15:11

Re: Function returning difference from 2 Variables

01 Dec 2021, 03:36

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,

Code: Select all

;;;;;;;;;function(not work);;;;;;;;;;
diference(val1, val2)
{
if (val1<val2)
  return val2 - val1 
if (val2<val1)
  return val1 - val2 
}
;;;;;;;;;;;;;;;;;;;;;;;;;;; 
so I removed them, and it works

Code: Select all

;;;;;;;;;function(works);;;;;;;;;;
diference(val1, val2)
{
if val1<val2
  return val2 - val1 
if val2<val1
  return val1 - val2 
}
;;;;;;;;;;;;;;;;;;;;;;;;;;; 
all I can say is my function does what I want
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Function returning difference from 2 Variables

01 Dec 2021, 03:42

armin889 wrote:
01 Dec 2021, 03:36
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
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).

all I can say is my function does what I want
Lucky guy ;) Look out for the actual (correct) syntax, if you plan to do more with AHK.
armin889
Posts: 96
Joined: 02 Nov 2021, 15:11

Re: Function returning difference from 2 Variables

01 Dec 2021, 04:02

Yes Greg
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 
}
well I am not understand how it works like this, maybe you can explain it?
gregster
Posts: 8921
Joined: 30 Sep 2013, 06:48

Re: Function returning difference from 2 Variables  Topic is solved

01 Dec 2021, 04:07

It's still redundant. This would be enough

Code: Select all

msgbox % diference(10,50) 
msgbox % diference(50,10)

diference(val1, val2)
{
  return val2 - val1 
}
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
armin889
Posts: 96
Joined: 02 Nov 2021, 15:11

Re: Function returning difference from 2 Variables

01 Dec 2021, 04:18

I got it now, It's less difficult than I thought

thanks for your help
User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Function returning difference from 2 Variables

01 Dec 2021, 13:35

flyingDman wrote:
01 Dec 2021, 01:03
Look at this: https://www.autohotkey.com/docs/Functions.htm#intro. You're almost there...
Change + into -
14.3 & 1.3.7

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Billykid, Google [Bot], Rohwedder and 210 guests