Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Default Parameter for Function as Variable


  • Please log in to reply
2 replies to this topic
fovic
  • Guests
  • Last active:
  • Joined: --
Hi, is it possible to use a variable for a function default parameter?

var := 123
test()

test(param = var)
{
	MsgBox % param
}
This doesn't work.

var := 123
test()

test(param = False)
{
	global
	if !param
		param := var
	MsgBox % param
}
I'm doing this instead but looklng for some simpler ways.

lilalurl.T32
  • Members
  • 391 posts
  • Last active: Jul 05 2011 03:39 PM
  • Joined: 17 May 2007
var := 123
test()

test(param = var)
{
   MsgBox % param
}

Not familiar with scripting functions but aren't you here defining param as an optional parameter?
Hence when you call test(), the optional parameter is not taken into account.

Also, not sure if a variable can be used as a default value for a parameter:

A parameter's default value must be one of the following: true, false, a literal integer, a literal floating point number, or a quoted/literal string such as "fox" or "" (but strings in versions prior to 1.0.46.13+ support only "").

(help file, function, optional parameters)
________
Kitchen Measures

Tyrsius
  • Members
  • 140 posts
  • Last active: Jun 20 2011 10:08 PM
  • Joined: 09 Jul 2009

Hi, is it possible to use a variable for a function default parameter?

var := 123
test()

test(param = var)
{
	MsgBox % param
}


You are doing this a bit backwards. You seem to want to pass a variable to a function, which is pretty much what a parameter is. it should look like this.

var=123 ; using the := means you are assigned var to the variables "123" and not the value
test (var) ; this will make param=var in the function "test"

test(param)
{
msgbox, %param% ; for the above, it will be "var"
}