473,544 Members | 1,959 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Use Named Arguments

ADezii
8,834 Recognized Expert Expert
When a call is made to a Sub or Function Procedure, you can supply Arguments in the exact order they appear in the Procedure's definition, or you can supply them in any position by name. To illustrate this point, I'll use a fictitious Function called fCalculateInter est() which accepts 3 Arguments (Currency, Single, and Long) and returns a value of type Currency.
Expand|Select|Wrap|Line Numbers
  1. Public Function fCalculateInterest(curPrincipal As Currency, sngRate As Single, lngTermInMonths As Long) As Currency
  2.   fCalculateInterest = curPrincipal * sngRate * lngTermInMonths
  3. End Function
  4.  
A simple Call to this Function would be:
Expand|Select|Wrap|Line Numbers
  1. Dim curInterest As Currency
  2. curInterest = fCalculateInterest(100000,.0725, 240)
  3.  
The above Function was called by supplying its Arguments in the correct positions, each delimited by a comma. If the positions of the Arguments change, you may either get a Data Type mismatch Error, or erroneous results. By the use of Named Arguments, you can supply the Arguments by name without any regard to position. A Named Argument consists of an Argument name followed by by a Colon and an Equal Sign (:=). We will now call that very same Function but this time reversing the positions of Arguments:
Expand|Select|Wrap|Line Numbers
  1. Dim curInterest As Curremcy
  2. curInterest = fCalculateInterest(lngTermInMonths:=240, sngRate:=.0725, curPrincipal:=100000)
  3.  
Named Arguments are extremely useful when you are calling a Procedure that has Optional Arguments. If you use Named Arguments, you don't have to include commas to denote missing position Arguments, if you don't use them, you must. The following Sub Procedure will illustrate my point, It accepts 4 Arguments, 3 of which are Optional (String, Long, Boolean).
Expand|Select|Wrap|Line Numbers
  1. Private Sub TestArgs(Arg1 As Integer, Optional Arg2 As String, Optional Arg3 As Long, Optional Arg4 As Boolean)
  2. End Sub
  3.  
A typical call to this Sub Procedure would be as follows:
Expand|Select|Wrap|Line Numbers
  1. Call TestArgs(1234, "Argument2", 999999, False)
  2.  
If you wanted to call TestArgs without passing the 2nd and 3rd Optional Arguments, and without using Named Arguments, the syntax would be:
Expand|Select|Wrap|Line Numbers
  1. Call TestArgs(1234,,,False)
If you wanted to call TestArgs without passing the 2nd and 3rd Optional Arguments, and use Named Arguments, without regard to the positions of the Arguments, the syntax would be:
Expand|Select|Wrap|Line Numbers
  1. Call TestArgs(Arg4:=False, Arg1:=1234)
NOTE: As a more realistic and practical example, and as suggested by Mary, I've included an example using the popular SendObject() Method. All Arguments of this method were used, except [TemplateFile], and their standard positions within this Method were modified. This was only possible by using this weeks Tip, Named Arguments:
Expand|Select|Wrap|Line Numbers
  1. DoCmd.SendObject OutputFormat:=acFormatXLS, ObjectName:="tblEmployeePFD", _
  2. ObjectType:=acSendTable, Subject:="Named Arguments", Bcc:="Joe Dunn", _ 
  3. To:="Tom Jones", CC:="Chris Myers", EditMessage:=False, _ MessageText:="Demonstration of the Usage of Named Arguments"
  4.  
The morale of the story is: Is is very advantageous to use Named Arguments, especially when utilizing Optional Arguments.
Nov 5 '07 #1
2 12508
missinglinq
3,532 Recognized Expert Specialist
Most useful info, ADezii, for two reasons!

First, there's all those functions that have a gazillion arguments, while usually only requiring

1 gazillion - (1 gazillion - 3) of those arguments!

And secondly, while it may take a nanosecond or two longer to enter the code, you can tell at a glance what the utilized parameters were used for! Far less cryptic!

Linq ;0)>
Nov 12 '07 #2
ADezii
8,834 Recognized Expert Expert
Most useful info, ADezii, for two reasons!

First, there's all those functions that have a gazillion arguments, while usually only requiring

1 gazillion - (1 gazillion - 3) of those arguments!

And secondly, while it may take a nanosecond or two longer to enter the code, you can tell at a glance what the utilized parameters were used for! Far less cryptic!

Linq ;0)>
Glad you like it Linq, one of my Favorites.
Nov 13 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1766
by: Fuzzyman | last post by:
A colleague and I have built a Validator object for use with ConfigObj and other general schema situations. A config file is used to store a schema that specifies how to test a value that it is valid. keyword=function(param1, param2) e.g. you could specify : size = range(30, 50) This means that the keyword 'size' must be an integer...
2
1532
by: Fuzzyman | last post by:
Sorry if this is a duplicate - I use the google interface and sometiems it screws up (not showing stuff you've posted *or* not posting it). Before you ask it's because at work I have no NNTP and *heavily* restricted http. A colleague and I have built a Validator object for use with ConfigObj and other general schema situations. A config...
48
3027
by: Adam Ruth | last post by:
Hello, Has there ever been any talk to adding named parameters to C? I enjoy using them in my Python and Ada code and can see their usefulness in C. I can envision an implementation where the naming would be based upon a prototype and the parameter ordering could be worked out before the linking phase (thus, no need to deal with changing...
5
1687
by: Booted Cat | last post by:
I've seen lots of discussions on the proposed inclusion of "function call with named arguments" to C/C++ on these newsgroups. My proposal is slightly different in that: * No ANSI approval is needed * No conflicts with existing language features such as function overloading * No need to modify the language spec or the compiler * No need to...
17
2139
by: Ben R. | last post by:
I'm reading about attribute classes and specifically, named versus positional parameters. Was this implimented instead of multiple constructors for flexibility or is there another reason I'm missing? -Ben
3
2607
by: Adam Hartshorne | last post by:
What is named parameter mechanism? Any ideas? I am looking through some code and there is a comment saying "VC++ has trouble with the named parameters mechanism", which i have no idea what this means. Any help much appreciated, Adam
36
2665
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called on it's own. However when I call the child object I get an error " has no properties (in firefox)." I simple test:
2
3319
by: Ramashish Baranwal | last post by:
Hi, I need to process few out of a variable number of named arguments in a function and pass the remaining to another function that also takes variable number of named arguments. Consider this simple example, def fun1(**kwargs): print kwargs.keys() def fun2(**kwargs):
2
2524
by: fahad.usman | last post by:
I am making an application in which if the second instance of the same application is launched, it checks its command-line arguments and passes them to the already running instance. I have been told by someone that using named pipes is one of the most commonly used ways of doing this. But I did it in a simpler way; I saved the command-line...
0
7387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7643
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7798
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7738
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5956
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5316
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1862
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.