Using A_screenHeight etc as optional parameters?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Using A_screenHeight etc as optional parameters?

03 Nov 2013, 00:49

I tried to create a function starting as follows:

Code: Select all

CalcFontSize(txt,fWd:=A_screenWidth,fHt:=A_screenWidth)
but got this error message:
Line Text: CalcFontSize(txt,fWd:=A_screenWidth,fHt:=A_screenWidth)
Error: Unsupported parameter default.
How to use A_screenWidth and A_ScreenHeight as optional parameters or is it impossible? Thanks.
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Using A_screenHeight etc as optional parameters?

03 Nov 2013, 00:53

As soon as I posted I found an answer:

Code: Select all

CalcFontSize(txt,fWd:=0,fHt:=0)
{ 
  if fWd = 0
  fWd := A_screenWidth
  if fHt = 0
  fHt := A_screenHeight
But is there a better way?
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: Using A_screenHeight etc as optional parameters?

03 Nov 2013, 01:18

Code: Select all

fn(w:="A_ScreenWidth", h:="A_ScreenHeight") {
	if (w == "A_ScreenWidth") ; if w is not  number
		w := %w%
	if (h == "A_ScreenHeight") ; if h is not number
		h := %h%
	MsgBox, % "Width: " w "`nHeight: " h
}

fn()
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Using A_screenHeight etc as optional parameters?

03 Nov 2013, 02:24

Thanks, that's an interesting alternative.
Wade Hatler
Posts: 60
Joined: 03 Oct 2013, 19:49
Location: Seattle Area
Contact:

Re: Using A_screenHeight etc as optional parameters?

08 Nov 2013, 14:02

I program in one other language that allows you to use expressions as optional parameters. It's a very cool feature, although in practice I don't use it as much as you would think - maybe 2-3% of my functions. The technique shown above works well.

What I usually do is reserve a special word like NADA to mean that the paramter wasn't passed, and compare to that. Using 0 or blank doesn't work because quite a bit of the time you actually want to pass a zero or blank as the real value. So I'd code your function as:

Code: Select all

    CalcFontSize(txt,fWd:="NADA",fHt:="NADA") {
        fWd := (fWd != "NADA" ? fWd : A_ScreenWidth)
        fHt := (fHt != "NADA"  ? fHt : A_ScreenHeight)
Of course, in cases where 0 is never a valid answer, you can simplify it to something like:

Code: Select all

    CalcFontSize(txt,fWd:="",fHt:="") {
        fWd := (fWd ? fWd : A_ScreenWidth)
        fHt := (fHt ? fHt : A_ScreenHeight)
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: Using A_screenHeight etc as optional parameters?

10 Nov 2013, 07:01

Thanks for the suggestion about NADA, you raise a good point about passing zero and genuine blank values.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: peter_ahk, zephyrus2706 and 369 guests