Newbie question (:=)

  • Thread starter Thread starter Bruce A. Julseth
  • Start date Start date
The := is used for named arguments in procedure calls. For example,
say you have a procedure like

Sub AAA (A As Integer, B As Integer, C As Integer, D As Integer)
' code
End Sub

Then, you want to call this procedure. You would normally use code
like

AAA 123, 345, 567, 678

In this line of code, it isn't readily clear what the number signify.
However, you can use named arguments to document the function call:

AAA A:=123, B:=345, C:=567, D:=678

This makes the code self-documenting, especially when the parameters
have names more descriptive than A, B, and C.

Also, with named arguments, you need not pass them in the order in
which they are declared in the called procedure. For example,

AAA D:=678, A:=123, C:=567, B:=345

is a perfectly valid function call even though the order of arguments
is D, A, C, B. The names cause the compiler to assign them correctly.

There is no reason to pass arguments out of order, and I would
recommend that you not do this, but it is possible.

In a function call, you can mix named arguments with unnamed
arguments. E.g.,

AAA 123, 345, C:=567, D:=678

However, all arguments after the first named argument must also be
named. E.g.,

AAA 123, 345, C:=567, 678

is invalid because C is named and D, which follows it, is not named.

All of the function calls to AAA are functionally equivalent. The
behave identically.


Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]
 
Thank you very much. Excellent explanation...I'm surprised my Excel book
didn't have anything like this. At least, I couldn't find anything.

Thanks again

Bruce

Chip Pearson said:
The := is used for named arguments in procedure calls. For example,
say you have a procedure like

Sub AAA (A As Integer, B As Integer, C As Integer, D As Integer)
' code
End Sub

Then, you want to call this procedure. You would normally use code
like

AAA 123, 345, 567, 678

In this line of code, it isn't readily clear what the number signify.
However, you can use named arguments to document the function call:

AAA A:=123, B:=345, C:=567, D:=678

This makes the code self-documenting, especially when the parameters
have names more descriptive than A, B, and C.

Also, with named arguments, you need not pass them in the order in
which they are declared in the called procedure. For example,

AAA D:=678, A:=123, C:=567, B:=345

is a perfectly valid function call even though the order of arguments
is D, A, C, B. The names cause the compiler to assign them correctly.

There is no reason to pass arguments out of order, and I would
recommend that you not do this, but it is possible.

In a function call, you can mix named arguments with unnamed
arguments. E.g.,

AAA 123, 345, C:=567, D:=678

However, all arguments after the first named argument must also be
named. E.g.,

AAA 123, 345, C:=567, 678

is invalid because C is named and D, which follows it, is not named.

All of the function calls to AAA are functionally equivalent. The
behave identically.


Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]




When do I use :=? I googled it didn't come up with anything.

Thanks..
 
Another benefit is when a Sub or Function has some
optional parameters. To modify Chip's example

Sub AAA(Optional A As Integer = 0, Optional B As Integer = 0, _
Optional C As Integer = 0, Optional D As Integer = 0)
' code
End Sub

Then you can write
AAAA A:=1, D:=42

rather than
AAAA 1, , , 42

and be sure you're using the right parameters and haven't
miscounted the commas.


Thank you very much. Excellent explanation...I'm surprised my Excel book
didn't have anything like this. At least, I couldn't find anything.

Thanks again

Bruce


The := is used for named arguments in procedure calls. For example,
say you have a procedure like
Sub AAA (A As Integer, B As Integer, C As Integer, D As Integer)
' code
End Sub
Then, you want to call this procedure. You would normally use code
like
AAA 123, 345, 567, 678
In this line of code, it isn't readily clear what the number signify.
However, you can use named arguments to document the function call:
AAA A:=123, B:=345, C:=567, D:=678
This makes the code self-documenting, especially when the parameters
have names more descriptive than A, B, and C.
Also, with named arguments, you need not pass them in the order in
which they are declared in the called procedure. For example,
AAA D:=678, A:=123, C:=567, B:=345
is a perfectly valid function call even though the order of arguments
is D, A, C, B. The names cause the compiler to assign them correctly.
There is no reason to pass arguments out of order, and I would
recommend that you not do this, but it is possible.
In a function call, you can mix named arguments with unnamed
arguments. E.g.,
AAA 123, 345, C:=567, D:=678
However, all arguments after the first named argument must also be
named. E.g.,
AAA 123, 345, C:=567, 678
is invalid because C is named and D, which follows it, is not named.
All of the function calls to AAA are functionally equivalent. The
behave identically.
Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]
On Tue, 5 Jan 2010 21:30:43 -0500, "Bruce A. Julseth"
 
Back
Top