473,703 Members | 3,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing arguments to function by reference - Good or Bad?

Just spent a happy 10 mins trying to understand a function I wrote sometime
ago.

Then remembered that arguments are passed by reference, by default.

Does the fact that this slowed me down indicate:

a) That I don't know enough

b) Passing arguments by ref is bad

c) A combination of the above.

Yours, Mike Macsween
Nov 13 '05 #1
39 7651
Personally, I don't think there's anything wrong with always learning (and
remembering again what you've learned before).
Surely everybody does in all walks of life.

And there are times when passing arguments by reference has been
considerably useful.

Others may disagree.

"Mike MacSween" <mi************ *************** @btinternet.com > wrote in
message news:42******** *************** @news.aaisp.net .uk...
Just spent a happy 10 mins trying to understand a function I wrote sometime ago.

Then remembered that arguments are passed by reference, by default.

Does the fact that this slowed me down indicate:

a) That I don't know enough

b) Passing arguments by ref is bad

c) A combination of the above.

Yours, Mike Macsween

Nov 13 '05 #2
rkc
Mike MacSween wrote:
Just spent a happy 10 mins trying to understand a function I wrote sometime
ago.

Then remembered that arguments are passed by reference, by default.

Does the fact that this slowed me down indicate:

a) That I don't know enough

b) Passing arguments by ref is bad

c) A combination of the above.


I've always thought that having byref be the default was, if not
bad, at least backwards.
Nov 13 '05 #3
"Mike MacSween" <mi************ *************** @btinternet.com > wrote in
message news:42******** *************** @news.aaisp.net .uk...
Just spent a happy 10 mins trying to understand a function I wrote
sometime ago.

Then remembered that arguments are passed by reference, by default.

Does the fact that this slowed me down indicate:

a) That I don't know enough

b) Passing arguments by ref is bad

c) A combination of the above.

Yours, Mike Macsween


By reference can cause you trouble. Look at the first sub, which might seem
OK at first glance. However, the TableExists function was written by some
idiot who changes the value of strTableName to "MSysAccessObje cts" if the
table is found. So although I would guess my sub says 'if the table is
there - delete it' what it actaually does is 'if the table is there try to
delete a system table'. Now although this example is a bit extreme and the
attempt to delete "MSysAccessObje cts" should fail anyway, I hope it
illustrates the general danger.

PS Was that your letter on BBC Radio 4's 'You and Yours' program a while ago
or just soemone with the same name?

Public Sub DeleteTable(str TableName As String)

On Error GoTo Err_Handler

If TableExists(str TableName) Then
CurrentDb.Table Defs.Delete strTableName
End If

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox Err.Description , vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub

Private Function TableExists(str TableName As String) As Boolean

On Error GoTo Err_Handler

Dim dbs As DAO.Database
Dim tdf As DAO.TableDef

Set dbs = CurrentDb

For Each tdf In dbs.TableDefs
If tdf.Name = strTableName Then
TableExists = True
strTableName = "MSysAccessObje cts"
Exit For
End If
Next tdf

Exit_Handler:

On Error Resume Next
Set tdf = Nothing
Set dbs = Nothing
Exit Function

Err_Handler:
MsgBox Err.Description , vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Function
Nov 13 '05 #4
Justin Hoffman wrote:
By reference can cause you trouble. Look at the first sub, which might seem
OK at first glance. However, the TableExists function was written by some
idiot who changes the value of strTableName to "MSysAccessObje cts" if the
table is found. So although I would guess my sub says 'if the table is
there - delete it' what it actaually does is 'if the table is there try to
delete a system table'. Now although this example is a bit extreme and the
attempt to delete "MSysAccessObje cts" should fail anyway, I hope it
illustrates the general danger.


It doesn't to me. Now, I'm suffering terribly from caffeine withdrawal
as I've abandoned my diet pepsi for Aquafina water, so I might be
missing something, but how else do you pass an argument to a function?

Other than making functions huge in size, calculating everything
redundantly for each function...

If I inherited and was maintaining the above example (code snipped) and
I didn't check to see where strTableName comes from (a simple search
project for the name of the function will work to find where the
function is embedded - menu items, are, of course a bit trickier) then I
deserve to have mSysAccessObjec ts deleted.... Ditto for a development
group leader that doesn't keep tabs on where the results of proc
development he's delegated out.

So, how else do you pass an argument? I hope I'm not sounding
confrontational (my apologies if I do), I just can't, at this moment
figure out how else to do it, unless it's something obvious I'm just
missing.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Nov 13 '05 #5
Per Mike MacSween:
Then remembered that arguments are passed by reference, by default.


Knowing nothing about the technical underpinnings, I pass parms ByVal whenever
possible.

Reason: somewhere in the few books I've read on "good" programming was the idea
that functions should be, among other things, independent. Seems to me like a
parm passed ByRef introduces the complication of somebody else being able to
affect the value in question.

Also, I always explicitly code ByVal or ByRef and push all the ByRefs to the end
of the parm list.
--
PeteCresswell
Nov 13 '05 #6
"Tim Marshall" <TI****@PurpleP andaChasers.Moe rtherium> wrote in message
news:d8******** **@coranto.ucs. mun.ca...
Justin Hoffman wrote:
By reference can cause you trouble. Look at the first sub, which might
seem OK at first glance. However, the TableExists function was written
by some idiot who changes the value of strTableName to
"MSysAccessObje cts" if the table is found. So although I would guess my
sub says 'if the table is there - delete it' what it actaually does is
'if the table is there try to delete a system table'. Now although this
example is a bit extreme and the attempt to delete "MSysAccessObje cts"
should fail anyway, I hope it illustrates the general danger.


It doesn't to me. Now, I'm suffering terribly from caffeine withdrawal as
I've abandoned my diet pepsi for Aquafina water, so I might be missing
something, but how else do you pass an argument to a function?

Other than making functions huge in size, calculating everything
redundantly for each function...

If I inherited and was maintaining the above example (code snipped) and I
didn't check to see where strTableName comes from (a simple search project
for the name of the function will work to find where the function is
embedded - menu items, are, of course a bit trickier) then I deserve to
have mSysAccessObjec ts deleted.... Ditto for a development group leader
that doesn't keep tabs on where the results of proc development he's
delegated out.

So, how else do you pass an argument? I hope I'm not sounding
confrontational (my apologies if I do), I just can't, at this moment
figure out how else to do it, unless it's something obvious I'm just
missing.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me

Hi Tim
A perfectly valid question - and if you want to really see it work, just
copy the example into a new module, create a dummy table and run DeleteTable
"MyTable" which stupidly tries to delete the system table.
Now alter the sub so that you put the word ByVal in front of the variable:
DeleteTable(ByV al strTableName As String)
Now when you run the sub, this can't happen. strTableName is passed by
value (just a copy - so the original can't be altered) and not by reference
where the sub may change the actual value.
Nov 13 '05 #7

"Justin Hoffman" <j@b.com> wrote in message
news:d8******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...
"Tim Marshall" <TI****@PurpleP andaChasers.Moe rtherium> wrote in message
news:d8******** **@coranto.ucs. mun.ca...
Justin Hoffman wrote:
By reference can cause you trouble. Look at the first sub, which might
seem OK at first glance. However, the TableExists function was written
by some idiot who changes the value of strTableName to
"MSysAccessObje cts" if the table is found. So although I would guess my
sub says 'if the table is there - delete it' what it actaually does is
'if the table is there try to delete a system table'. Now although this
example is a bit extreme and the attempt to delete "MSysAccessObje cts"
should fail anyway, I hope it illustrates the general danger.


It doesn't to me. Now, I'm suffering terribly from caffeine withdrawal
as I've abandoned my diet pepsi for Aquafina water, so I might be missing
something, but how else do you pass an argument to a function?

Other than making functions huge in size, calculating everything
redundantly for each function...

If I inherited and was maintaining the above example (code snipped) and I
didn't check to see where strTableName comes from (a simple search
project for the name of the function will work to find where the function
is embedded - menu items, are, of course a bit trickier) then I deserve
to have mSysAccessObjec ts deleted.... Ditto for a development group
leader that doesn't keep tabs on where the results of proc development
he's delegated out.

So, how else do you pass an argument? I hope I'm not sounding
confrontational (my apologies if I do), I just can't, at this moment
figure out how else to do it, unless it's something obvious I'm just
missing.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me

Hi Tim
A perfectly valid question - and if you want to really see it work, just
copy the example into a new module, create a dummy table and run
DeleteTable "MyTable" which stupidly tries to delete the system table.
Now alter the sub so that you put the word ByVal in front of the variable:
DeleteTable(ByV al strTableName As String)
Now when you run the sub, this can't happen. strTableName is passed by
value (just a copy - so the original can't be altered) and not by
reference where the sub may change the actual value.

Sorry - the ByVal should of course appear in the TableExists function, as
this was the naughty piece of coding which was altering the value:
Private Function TableExists(ByV al strTableName As String) As Boolean
Although having it in the DeleteTable wouldn't be a bad idea either.
Nov 13 '05 #8
Justin Hoffman wrote:
Hi Tim
A perfectly valid question - and if you want to really see it work, just
copy the example into a new module, create a dummy table and run
DeleteTable "MyTable" which stupidly tries to delete the system table.
Now alter the sub so that you put the word ByVal in front of the variable:
DeleteTable(B yVal strTableName As String)
Now when you run the sub, this can't happen. strTableName is passed by
value (just a copy - so the original can't be altered) and not by
reference where the sub may change the actual value.


Sorry - the ByVal should of course appear in the TableExists function, as
this was the naughty piece of coding which was altering the value:
Private Function TableExists(ByV al strTableName As String) As Boolean
Although having it in the DeleteTable wouldn't be a bad idea either.


Ah, OK, thanks for the clarification. I thought we were talking about
arguments, period! 8)

--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Nov 13 '05 #9
On Tue, 7 Jun 2005 10:42:36 +0100, "Mike MacSween"
<mi************ *************** @btinternet.com > wrote:
Just spent a happy 10 mins trying to understand a function I wrote sometime
ago.

Then remembered that arguments are passed by reference, by default.

Does the fact that this slowed me down indicate:

a) That I don't know enough

b) Passing arguments by ref is bad

c) A combination of the above.

Yours, Mike Macsween


My 2c on ByRef.

Well, yes, passing parameters by reference by default is somewhat dangerous.
I think it is the default, though, because it is also generally more
efficient. That doesn't make it right, but it might explain why MS did it
that way, particularly since computers were a lot slower when VB 1.0 came out.

For a programmer with good habits (well - if -all- programmers who maintain
the project have good habits), the passing of parameters by reference should
not be a problem.

The reason I say this is that it is bad style to ever write a value to a
parameter except in the odd case where you do actually choose to use ByRef for
returning a value to the caller, which is sometimes appropriate. The reason
for this rule is that one variable should never do more than one thing. If
one variable is both a parameter for getting a value into a procedure and a
buffer to hold working data within the procedure, that's more than one thing.

Now - notice the part above about "if -all- programmers who maintain the
project ..."? Well, we should never strive to write programs that are only
easy to maintain by experts. That's why it might be good to just not use
ByRef parameters unless you mean it or unless you are optimizing a slow part
of the code. In special cases, such as when processing strings in a loop, you
might preemptively choose to pass string parameters by reference to save the
hassle of fighting with performance problems later.
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6371
by: Martin Lucas-Smith | last post by:
Having re-read www.php.net/functions.arguments recently, the notion of passing arguments by reference to a function makes a lot more sense now. However, my question is: is there any difference in outcome between: function add_some_extra(&$string) { $string .= 'and something extra.'; }
4
3599
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back in my variables ?? note1: I'm not the one who wrote the web service.. so I can't modify
20
1989
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> bool1=True lst1= def func1(arg1): arg1.append(4)
12
2680
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
4
52785
by: Scott M. | last post by:
Does C# support passing arguments ByVal & ByRef? If so, what is the default (ByVal?)? And, if so, how do you explicitly specify either? Thanks.
3
1194
by: pkirk25 | last post by:
// get the item, profession and its mats int get_bill_of_materials(map<string, string&mats_map); map<string, stringcrafted_items; int z = get_bill_of_materials(crafted_items); Have I passed by reference or so I need to do this? map<string, stringcrafted_items;
0
1344
by: Gonçalo Rodrigues | last post by:
Hi all, I have a single-rooted hierarchy of heap-allocated objects -- call the root Object. These objects are handled via a smart pointer template Reference<T>. Basically, Reference<Tis a wrapper around T* with some extra smarts including reference counting to make memory management easier. Below, Reference<Objectwill be denoted simply by Ref. Since you only get your hands on Reference<Tand never on a T (or T*) directly, my thumb rule...
10
13045
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var boldLink=document.getElementById('cmtbold');
1
8805
by: 418928 | last post by:
Hi everybody, I understand that the standard way of passing arguments to a web page using javascript is "<url>?<arguments separated with "&" and values with "=">. Then, I can use window.location.search and the split function to obtain the arguments. However, is there any way of interpreting part of a URL as arguments for Javascript? For example, would it be possible to use "http://www.myURL.com/idClient/1233" instead of...
0
8761
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8674
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9262
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9125
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7876
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3127
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
2
2463
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2070
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.